Android下的WebView的使用

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/");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值