[Android development] WebView related usage

Commonly used tool classes in WebView:
(1) WebSettings: manage the settings of WebView, such as whether to allow JS execution

(2) WebViewClient class:
Function: handle various notification & request events

Common method 1: shouldOverrideUrlLoading()
does not call the system browser when opening the URL, but displays it in this WebView; all loading on the web page goes through this method

//步骤1. 定义Webview组件
Webview webview = (WebView) findViewById(R.id.webView1);

//步骤2. 选择加载方式
  //加载一个网页
  webView.loadUrl("http://www.google.com/");

  //加载apk包中的html页面
  webView.loadUrl("file:///android_asset/test.html");

  //加载手机本地的html页面
   webView.loadUrl("content://com.android.htmlfileprovider/sdcard/test.html");

//步骤3. 复写shouldOverrideUrlLoading()方法,使得打开网页时不调用系统浏览器,而是在本WebView中显示
    webView.setWebViewClient(new WebViewClient(){
    
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    {
    
    
        view.loadUrl(url);
        return true;
    }
  });

Common method 2: When onPageStarted()
starts loading the page, we can set a loading page to tell the user that the program is waiting for the network response.

   webView.setWebViewClient(new WebViewClient(){
    
    
     @Override
     public void  onPageStarted(WebView view, String url, Bitmap favicon) {
    
    
        //设定加载开始的操作
     }
 });

Guess you like

Origin blog.csdn.net/qq_39441603/article/details/131298591