WebView的简单用法

简单的加载一张网页

     mWebView = new WebView(this);
        setContentView(mWebView);
        //添加这句话,否则会调用第三方浏览器打开
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.loadUrl("http://www.baidu.com");
<manifest >
    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

在WebView中启用JavaScript交互

1.启用JavaScript

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

2.定义类,用于JavaScript交互
public class WebAppInterface {
Context mContext;

/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
    mContext = c;
}

/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
    Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}

}
注意:@JavascriptInterface 这个必须添加
3.向WebView添加用于交互的对象&&给网页中JavaScript脚本需要的对象名称“Android”

webView.addJavascriptInterface(new WebAppInterface(this), "Android");

4.在assets文件中定义一个hello.html网页,内容如下:

<html>
<body>
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript"> function showAndroidToast(toast) { Android.showToast(toast); } </script>
</body>
</html>

5.JavaScript&&Android交互的完整java代码如下:
注意assets文件夹的位置

public class MainActivity extends Activity {
    WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWebView = new WebView(this);
        setContentView(mWebView);
        //添加这句话,否则会调用第三方浏览器打开
        mWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings=mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new WebAppInterface(this),"Android");
        mWebView.loadUrl("file:///android_asset/hello.html");
    }

    public class WebAppInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /** Show a toast from the web page */
        @JavascriptInterface
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }


}

处理WebView中链接的打开方式问题

启用JavaScript带来便捷的同时引入了安全问题,如果可以确保跳转的链接是安全的可以使用WebView,如果不能保证则使用默认浏览器进行处理。

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

修改

        mWebView.setWebViewClient(new WebViewClient());
为
        mWebView.setWebViewClient(new MyWebViewClient ());

WebView浏览历史的回退

这个没有什么好解释的,就是可以查看浏览历史进行前进&&回退,放一个Demo,大家理解一下吧。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

    <LinearLayout  android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal">

        <Button  android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="goBack" />

        <Button  android:id="@+id/btn_forward" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="goForward" />

    </LinearLayout>


    <WebView  android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/ll"></WebView>

</RelativeLayout>

public class MainActivity extends Activity implements View.OnClickListener {
    WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.webView);
        findViewById(R.id.btn_back).setOnClickListener(this);
        findViewById(R.id.btn_forward).setOnClickListener(this);

        //添加这句话,否则会调用第三方浏览器打开
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.loadUrl("http://www.baidu.com");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back: {
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                }
                break;
            }
            case R.id.btn_forward: {
                if (mWebView.canGoForward()) {
                    mWebView.goForward();
                }
                break;
            }
            default:
                break;

        }
    }

}

做WebView调试的时候踩了一个坑,url必须添加http://,即使是IP

翻译地址

https://developer.android.com/guide/webapps/webview.html

你可能感兴趣的:(组件)