您的位置:首页 > 移动开发

Android下的WebView的使用

2016-03-10 11:22 134 查看

what is WebView

官方解释: A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It use the WebKit rendering engine to display web pages and includes methods to navigate forword and backward through a history, zoom in and out, perform text searched and more.

Basic usage 基本用法

By default, a WebView provides no browser-like widgets, does not enable JavaScript and web page errors are ignored. If your goal is only to display some HTML as a part of your UI, this is probable fine;the user won’t need to interact the web page beyond reading it, and the web page won’t need to interact with the user.If you actually want a full-blown web browser, then you probably want to invoke the Browser appplication with a URL Intent rather than show it with a WebView. For eample:

//意图打开访问连接
Uri uri = Uri.parse("http://www.example.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

To provide a WebView in your own Activity, include a WebView in your layout ,or set the entire Activity window as a WebView during onCreate():

//代码使用WebView
WebView webview = new WebView(this);
setContentView(webview);

Then load the desired web page:

// Simplest usage: note that an exception will NOT be thrown
// if there is an error loading this page(see below)
webview.loadUrl("http://www.baidu.com");

a WebView has several customization points where you can add your own behavior. These are:

  1. Creating and setting a WebChromeCLient subclass. This clas is called when something that might impact a a browser UI happens, for instance, progress updates and JavaScript alerts are sent here.
  2. Creating and setting a WebVIewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions.You can also intercept URL loading here.
  3. Modifying the WebSettings, such as enabling JavaScript with setJavaScriptEnabeld().
  4. Injecting Java objexts into the WebView using the addJavaScriptInteface(Obejct, String) method.This method allows you to inject Java objects into a page’s JavaScript context, so that they can be accessed by JavaScript in the page.
// Let's display the progress in the activity title bar, like the browser app does
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});

webview.loadUrl("http://www.baidu.com/");
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: