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:
- 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.
- 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.
- Modifying the WebSettings, such as enabling JavaScript with setJavaScriptEnabeld().
- 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/");阅读更多
相关文章推荐
- Android中webview的使用
- android 使用webview.copyBackForwardList()来进行判断是否是在当前页打开的链接
- Android WebView使用
- 让python在android系统上飞一会儿:第四节 使用WebView制作程序界面
- Android-----WebView使用
- Android WebView使用
- Android之WebView使用技巧
- android.webkit.WebView 使用 SINGLE_COLUMN layout algorithm 导致程序意外退出的解决办法
- android中WebView的简单使用
- android webView的使用方法
- Android WebView使用
- Android-----WebView使用
- android webview的使用
- Android WebView的使用
- Android中WebView控件的基本使用
- Android——WebView组件的使用详解
- android WebView使用cmwap无法联网解决办法
- Android中在WebView中使用javascript
- [转]android 使用WebView
- Android-----WebView使用