最近一直在写webView的东西,发现有好多功能点,记一下。
JS调用Android的方法:
在JS中:写 window.JavaScriptinterface.closecurrent();
在Adnriod中:
webView.addJavascriptInterface(new backJS(), "JavaScriptinterface");
注意:new backJS()名字可以随意取,“JavaScriptinterface”对应JS
class backJS {
//没有返回参数
@JavascriptInterface
public void closeCurrentWin() {
//内容为要调用的方法
}
//返回一个String类型参数
@JavascriptInterface
public void closeCurrentWin(String flash) {
//内容为要调用的方法
}
}
webView下拉刷新(SwipeRefreshLayout)
使用android.support.v4.widget.SwipeRefreshLayout进行下拉刷新,如果使用过程中找不到包报错,替换一下support.v4就好了,也可以点击链接下载直接替换
http://download.csdn.net/detail/sunflower_12/9806495
1、xml
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container_links"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webView_link"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</android.support.v4.widget.SwipeRefreshLayout>
2、初始化 (设置监听和进度条颜色)
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container_links);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(R.color.greys_1, R.color.greys_2,
R.color.greys_3, R.color.greys_4);
3、刷新
@Override
public void onRefresh() {
// 重新刷新页面
webView_link.loadUrl(webView_link.getUrl());
}
下载文件
1、设置监听
webView.setDownloadListener(new MyWebViewDownLoadListener());
2、下载到sd卡的指定目录
private class MyWebViewDownLoadListener implements DownloadListener {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast t = Toast.makeText(LinkActivity.this, "需要SD卡。",
Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
return;
}
loadFiles(upUrl);//根据URl下载即可
}
}
注意:在使用webView上传图片时,正常是单张上传,若想同时上传多个图片,要自定义,同时JS中写个按钮,