Android WebView控件

前言

WebView主要用来显示网页,采用Webkit内核。

1. 访问网页

添加权限

<uses-permission android:name="android.permission.INTERNET"/>

布局文件

<WebView
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

加载网页

WebView webView = findViewById(R.id.web_view);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("http://www.baidu.com");

2. WebView主要方法

加载网页,url为网页链接地址,data是网页内容。

// 加载URL指定的网页
public void loadUrl(String url)
// 携带http headers加载URL指定的网页
public void loadUrl(String url, Map<String, String> additionalHttpHeaders)
// 加载内容
public void loadData(String data, String mimeType, String encoding)
public void loadDataWithBaseURL(String baseUrl, String data,
        String mimeType, String encoding, String historyUrl)
// 使用POST请求加载指定的网页
public void postUrl(String url, byte[] postData)
// 重新加载
public void reload()

post()方式访问网页,

mWebView.postUrl(url, new byte[0]);
mWebView.postUrl(url, EncodingUtils.getBytes(postData, "utf-8"))

跳转接口

public boolean canGoBack()
// 返回上一页
public void goBack()
public boolean canGoForward()
// 返回下一页
public void goForward()

清空缓存

// 清除网页缓存,由于内核缓存是全局的因此这个方法不仅仅针对webview而是针对整个应用程序
public void clearCache(boolean includeDiskFiles)
// 清除自动完成填充的表单数据
public void clearFormData()
// 清楚历史
public void clearHistory()

3. 主要辅助类

WebViewClient

// 拦截加载页面
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
// 页面(url)开始加载
public void onPageStarted(WebView view, String url, Bitmap favicon)
// 页面(url)完成加载
public void onPageFinished(WebView view, String url)
// 将要加载资源(url)
public void onLoadResource(WebView view, String url)

WebChromeClient

// 接收当前页面的加载进度
public void onProgressChanged(WebView view, int newProgress)
// 接收文档标题
public void onReceivedTitle(WebView view, String title)

WebSetting

// 是否支持Javascript
settings.setJavaScriptEnabled(true);
// 缓存模式
settings.setCacheMode(WebSettings.LOAD_DEFAULT);

4. JS回调和调用JS

web_view.html文件,编写一个html文件放置在assets目录下

<html>
<head>
    <title>Android Demo</title>
    <script>
        function myFunction() {
            document.getElementById("demo").innerHTML="My First JavaScript Function";
        }
    </script>
</head>
<body>
    <label id="demo">Hell World!</label>
    <button type="button" onclick="window.jsListener.onCall()">JS CallBack</button>
</body>
</html>

设置WebViewWebSettings,使得javascript能够运行。

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

添加js监听,addJavascriptInterface(Object, String)添加一个监听器,

mWebView.addJavascriptInterface(new JSInterface(), "jsListener");
mWebView.loadUrl("file:///android_asset/web_view.html");

JSInterface中的onCall()方法可以被调用,可以看到在js调用中,window.jsListener.onCall()与android里面的名字一一对应。

public class JSInterface {
    @JavascriptInterface
    public void onCall() {                
    }
}

android中调用js

mWebView.loadUrl("javascript:myFunction()");

效果如下
在这里插入图片描述

5. 文件功能

WebChromeClient中添加openFileChooser()onShowFileChooser()方法。

mWebView.setWebChromeClient(new WebChromeClient() {

    // For Android 3.0+  
    public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType) {
        openFileChooser(uploadFile);
    }

    // For Android < 3.0
    public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType,
               String capture) {
        openFileChooser(uploadFile);
    }

    // For Android > 4.1.1
    public void openFileChooser(ValueCallback<Uri> uploadFile) {
        mValueCallback = uploadFile;
        showFile();
    }
	
    // For Android > 5.0
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
            FileChooserParams fileChooserParams) {
        mFilePathCallback = filePathCallback;
        showFile();
        return true;
    }

});

选中文件后返回

mValueCallback.onReceiveValue(uri)
mFilePathCallback.onReceiveValue(new Uri[]{uri})

或者返回null表示取消

mValueCallback.onReceiveValue(null)
mFilePathCallback.onReceiveValue(null)

参考资料:https://www.jianshu.com/p/a6f7b391a0b8
参考资料:http://blog.csdn.net/atangsir/article/details/51388662

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值