WebView usage summary and WebView usage Summary
1) Add permission: The permission "android. permission. INTERNET" must be used in AndroidManifest. xml; otherwise, the Web page not available error may occur.
2) generate a WebView component in the Activity: WebView webView = new WebView (this); or add the webview control to the layout file of the activity:
<WebView
Android: id = "@ + id/wv"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "@ string/hello"
/>
3) set basic WebView information:
If the accessed page contains Javascript, webview must be set to support Javascript.
Webview. getSettings (). setJavaScriptEnabled (true );
Touch focus Function
RequestFocus ();
Cancel scroll bar
This. setScrollBarStyle (SCROLLBARS_OUTSIDE_OVERLAY );
4) set the web page to be displayed in WevView:
For Internet: webView. loadUrl ("http://www.google.com ");
Local file: webView. loadUrl ("file: // android_asset/XX.html"); local files are stored in the assets file.
5)
If you want to click the link for processing by yourself, instead of the link in the browser of the new Android system.Add an event listening object (WebViewClient) to WebView and override some of the methods:
ShouldOverrideUrlLoading: Response to the hyperlink button on the webpage. When you press a connection, WebViewClient will call this method and pass the parameter: the url to be pressed. For example, when a number of embedded web pages in webview is clicked, it will automatically consider this as a telephone request and pass the url: tel: 123, if you do not want this, you can rewrite the shouldOverrideUrlLoading function to solve the problem:
Public boolean shouldOverrideUrlLoading (WebView view, String url) {if (url. indexOf ("tel:") <0) {// a number is displayed on the page, causing the connection call view. loadUrl (url);} return true ;}
6) to allow WebView to load assets from the apk file, the Android SDK provides a schema prefixed with "file: // android_asset /". When WebView encounters such a schema, it finds the content in the assets Directory of the current package. Above "file: // android_asset/demo.html"
7) the Java object and method to be bound in the addJavascriptInterface method must run in another thread and cannot be run in the Construction thread. This is also the purpose of using Handler.
8. When the div layer is used in webview, overlapping operations may occur. You can only use the dom method to delete the div.
Webview and js Interaction
wv.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");private final class DemoJavaScriptInterface { DemoJavaScriptInterface(){} public void clickonAndroid( final String order){ mHandler.post(newRunnable(){ @Override public void run(){ jsonText="{"name":""+order+""}"; wv.loadUrl("javascript:wave("+jsonText+")"); } }); }}
WebView is a view that displays Web pages. It is based on your web browser or content that is only displayed in your Activity.
It uses the WebKit rendering engine to display webpages. It includes some methods: browsing records forward and backward, enlarging, downgrading, and text search.
Implement built-in zoom in and out using WebSettings. setBuiltInZoomControls (boolean ).
Webview for android Development
If it is convenient, directly paste the reported error and the error code, which may help solve the problem more!
In Android development, the WebView component is used to display web pages, while clicking content in the web page opens the browser browsing. How does one default WebView browsing?
If you want to click the link for processing by yourself, instead of the link in the browser of the new Android system. Add an event listening object (WebViewClient) to WebView and rewrite some of the Methods: shouldOverrideUrlLoading: Response to the hyperlink button in the webpage. When you press a connection, WebViewClient will call this method and pass the parameter: the url to be pressed.
WebView. setWebViewClient (new WebViewClient (){
@ Override
Public boolean shouldOverrideUrlLoading (WebView view, String url ){
View. loadUrl (url); // jump to the new url in the current webview
Return true;
}
});