Simple to use WebView

The only way to define the layout file in good webView, listening in Java code, webview itself is a browser, Google built-in browser, so you can load the page directly, webview also use the JavaScript scripting language

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

 <WebView
     android:id="@+id/webView"
     android:layout_width="match_parent"
     android:layout_height="500dp"></WebView>

</RelativeLayout>

The above is the layout file

public class MainActivity extends AppCompatActivity {
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       webView = findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://www.baidu.com/?tn=62095104_19_oem_dg");
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                System.out.println("开始加载");
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                System.out.println("结束加载");
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                System.out.println("显示标题");
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                System.out.println("显示加载进度" + newProgress + "%");
            }
        });
    } 

    @Override 
    public  boolean onKeyDown ( int keyCode, KeyEvent Event) {
         IF (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack ()) { 
            webView.goBack (); // Return to the previous page instead of exiting the browser 
            return  to true ; 
        } 

        return  Super .onKeyDown (the keyCode, Event); 
    } 


    @Override 
    protected  void onDestroy () {
         IF (! the webView = null ) { 
            webView.loadDataWithBaseURL ( null , "", "text / HTML", "UTF-. 8", null );
            webView.clearHistory();    //销毁Webview

            ((ViewGroup) webView.getParent()).removeView(webView);
            webView.destroy();
            webView = null;
        }
        super.onDestroy();
    }

}

Finally, do not forget to network rights

Guess you like

Origin www.cnblogs.com/Ocean123123/p/10960624.html