网站首页 > 博客文章 正文
由于现如今H5的热门,做过不少与H5的交互工作了,现在总结一下。
初始化WebView
/**
* 初始化WebView
*/
private void initWebView() { // 设置setWebChromeClient对象
mWb_main.setWebChromeClient(new WebChromeClient() { @Override
public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); //设置本地的ToolBar标题
mTv_main.setText(title);
}
}); //打开网页时不调用系统浏览器, 而是在本WebView中显示
mWb_main.setWebViewClient(new WebViewClient() { @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url); return true;
}
});
WebSettings webSettings = mWb_main.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDefaultTextEncodingName("UTF-8");//设置编码
webSettings.setUseWideViewPort(true);//设置此属性,可任意比例缩放webSettings.setLoadWithOverviewMode(true);
}
Android 调用JS的无参数方法
在HTML5中的header中加入这段代码
<script>
//这是被Android调用的JS方法function noParamFunction() {document.getElementById("noparam_ta").style.fontSize =40+"px";
}</script>
然后在Android里边我们调用
/**
* Android 调用 JS代码
*/
mButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
mWb_main.loadUrl("javascript:noParamFunction();");
}
});
Android 调用JS的有参数方法
在HTML5中的header中加入这段代码
<script>
//这是被Android调用的JS方法function noParamFunction() {
document.getElementById("noparam_ta").innerHTML=data;
}</script>
然后在Android里边我们调用的时候需要加入‘”+str+”’ 这种形式的,才可以想HTML传递参数
/**
* Android 调用 JS代码
*/
String str = "Hello JS"
mButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
mWb_main.loadUrl("javascript:noParamFunction('"+str+"');");
}
});
JS调用Android 代码
首先我们要定义一个类供JS调用,这里需要注意在4.2以前,不需要在方法前加上 @JavascriptInterface,4.2以后的系统就需要加上了,代码如下
public class JavaScriptinterface {
private Context mContext; /** Instantiate the interface and set the context */
public JavaScriptinterface(Context c) {
mContext = c;
}/**
* 安卓系统4.2以上的系统需要加上 @JavascriptInterface,才可以让webview读取自己的方法
* @param toast
*/
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
Log.e("----","--------------------------------------------showToast");
}
}
然后在HTML中header里边加入script代码
<script>
//这是js调用Android的方法
function showAndroidToast(str) {
javascript:window.android.showToast(str);
}</script>
在Body标签加入
<input type="button" value="调用安卓的方法" onClick="showAndroidToast('调用成功')" />
然后在我们Android 端写入如下代码就可以调用了
/**
* JS 调用 Android 代码
*/
mWb_main.addJavascriptInterface(new JavaScriptinterface(this), "android");1234512345
Android读取html的标题
// 设置setWebChromeClient对象
mWb_main.setWebChromeClient(new WebChromeClient() { @Override
public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); //设置本地的ToolBar标题
mTv_main.setText(title);
}
});
Android允许Html的Alert()对话框
// 设置setWebChromeClient对象
mWb_main.setWebChromeClient(new WebChromeClient() { //处理javascript中的alertpublic boolean onJsAlert(WebView view, String url, String message, final JsResult result) { //构建一个Builder来显示网页中的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Alert");
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
builder.setCancelable(false);
builder.create();
builder.show(); return true;
} //处理javascript中的confirm
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("confirm");
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
builder.setCancelable(false);
builder.create();
builder.show(); return true;
}
});
猜你喜欢
- 2025-04-26 微信小程序Webview上传图片闪退
- 2025-04-26 获取当前webview的URL(Uniapp必会)
- 2025-04-26 Android WebView远程执行代码漏洞浅析
- 2025-04-26 通过分享的链接无法打开app?
- 2025-04-26 您使用的App是用什么技术开发
- 2025-04-26 SpringBoot系列——基于mui的H5套壳APP开发web框架
- 2025-04-26 2个将HTML5打包成app的方法
- 2025-04-26 苹果,自家后院着火了
- 2025-04-26 为什么你做的H5开屏那么慢?H5首屏秒开方案探讨
- 2025-04-26 openinstall:微信小程序跳转H5,配置业务域名教程
你 发表评论:
欢迎- 07-08Google Cloud Platform 加入支持 Docker 的容器引擎
- 07-08日本KDDI与Google Cloud 签署合作备忘录,共探AI未来
- 07-08美国Infoblox与Google Cloud合作推出云原生网络和安全解决方案
- 07-08GoogleCloud为Spanner数据库引入HDD层,将冷存储成本降低80%
- 07-08谷歌推出Cloud Dataproc,缩短集群启动时间
- 07-08Infovista与Google Cloud携手推进射频网络规划革新
- 07-08比利时Odoo与Google Cloud建立增强合作,扩大全球影响力
- 07-08BT 和 Google Cloud 通过 Global Fabric 加速 AI 网络
- 最近发表
-
- Google Cloud Platform 加入支持 Docker 的容器引擎
- 日本KDDI与Google Cloud 签署合作备忘录,共探AI未来
- 美国Infoblox与Google Cloud合作推出云原生网络和安全解决方案
- GoogleCloud为Spanner数据库引入HDD层,将冷存储成本降低80%
- 谷歌推出Cloud Dataproc,缩短集群启动时间
- Infovista与Google Cloud携手推进射频网络规划革新
- 比利时Odoo与Google Cloud建立增强合作,扩大全球影响力
- BT 和 Google Cloud 通过 Global Fabric 加速 AI 网络
- NCSA和Google Cloud合作开发AI驱动的网络防御系统,加强泰国网络空间的安全性
- SAP将在沙特阿拉伯 Google Cloud 上推出BTP服务
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- messagesource (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)