android:WebView usage

How to create a WebView:     1. Add permission: The permission must be used in AndroidManifest.xml: "android.permission.INTERNET", otherwise there will be a Web page not available error.     2. Create a WebView instance: WebView webView = new WebView(this);      3. WebView basic settings:            webview.getSettings().setJavaScriptEnabled(true); //Setting support for Javascript            webView.getSettings().setBuiltInZoomControls(true); // Add a zoom button to the page            webView.requestFocus(); //Touch focus works. If it is not set, when clicking the text input box of the webpage, the soft keyboard cannot pop up and it does not respond to some other events.           webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); //Cancel the scroll bar      4. Set the web page to be displayed by WevView:            Internet use: webView.loadUrl("http://www.google.com");            Local file use: webView.loadUrl ("file:///android_asset/XX.html"); The local file is stored in: assets file  















    5. If you want to click on the link, it will be handled by yourself, instead of responding to the link in the browser of the newly opened Android system. 
          Add an event listener object (WebViewClient) to WebView      
          and override some of the methods 
                shouldOverrideUrlLoading: the response to the hyperlink button in the web page. 
                                          When a connection is pressed, WebViewClient will call this method and pass parameters: pressed url 
                onLoadResource   
                onPageStart  
                onPageFinish  
                onReceiveError 
                onReceivedHttpAuthRequest 

    6. If you use the webview point link to jump to multiple pages, if you do not do anything, click the system "Back" key, the entire browser will call finish() to end itself. If you want the web page you browse to roll back instead of exiting the browser, you need to process and consume the Back event in the current Activity. 
       Override the onKeyDown(int keyCoder, KeyEvent event) method of the Activity class.

Java code   收藏代码
  1. public boolean onKeyDown(int keyCoder,KeyEvent event) {    
  2.     if ( webView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK ) {    
  3.         webView.goBack();   //返回webView的上一页面    
  4.         return true;    
  5.     }    
  6.     return false;    
  7. }    



WebView相关问题注意: 

Android的webView很强大,其实就是一个浏览器,你可以把它嵌入到你想要的位置,我这里遇到两个问题,一是怎么知道网页的加载进度,二是加载网页时,点击网页里面的链接还是在当前的webview里跳转,不想跳到浏览器那边。解决办法如下:

Java代码   收藏代码
  1. webView.setWebChromeClient(new WebChromeClient() {    
  2.     public void onProgressChanged(WebView view, int progress) {    
  3.         setTitle("页面加载中 ... " + progress + "%");    
  4.         setProgress(progress * 100);    
  5.         if (progress == 100) {    
  6.             setTitle(R.string.app_name);    
  7.         }    
  8.     }    
  9. });    
  10.   
  11. webView.setWebViewClient(new WebViewClient() {    
  12.     @Override  
  13.     public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  14.         //设置点击网页里面的链接还是在当前的webview里跳转  
  15.         view.loadUrl(url);    
  16.         return true;    
  17.     }    
  18.     @Override  
  19.     public void onReceivedSslError(WebView view,   
  20.             SslErrorHandler handler, android.net.http.SslError error) {   
  21.         //设置webview处理https请求  
  22.         handler.proceed();  
  23.     }  
  24.     public void onReceivedError(WebView view,  
  25.             int errorCode, String description, String failingUrl) {  
  26.         //加载页面报错时的处理  
  27.         Toast.makeText(MainActivity.this,   
  28.                 "Oh no! " + description, Toast.LENGTH_SHORT).show();  
  29.     }  
  30. });    

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326685022&siteId=291194637