WebView和加载html页面时无法调用alert的解决办法
2016-01-13 21:50
543 查看
最近在做项目时,用到了webview 加载h5,调试时需要通过 alert来检测获取的数据的正确性,开始怎么弄alert在android 手机上就是不出来,后面才解决。现将demo放上,
一般通过webview 加载 html 页面时代码是这么写的
这样可以正常的和H5中js交互(本地、服务器一样),但是通过alert来调试数据时,一直没有作用。
通过查API,知道 有个setWebChromeClient的方法,官方解释为Sets the chrome handler. This is an implementation of WebChromeClient for use in handling JavaScript dialogs, favicons, titles, and the progress.This will replace the current handler. 大体意思是这个方法是来处理 javascript 方法中的对话框,收藏夹,标题和进度条之类的。
我所要用的alert刚好属于 dialogs 一类。
将代码中“*****”
就可以显示了。
一般通过webview 加载 html 页面时代码是这么写的
public class JSWebviewFragment extends BaseFragment { @Override public String getTAG() { return "JSWebviewFragment"; } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { WebView webView = new WebView(getActivity()); webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); webView.setWebViewClient(new WebViewClient()); // ****** webView.addJavascriptInterface(this, "jswebview"); webView.loadUrl("file:///android_asset/test.html"); return webView; } @JavascriptInterface public void androidMethod(String txt) { showToastShort(txt); LogUtils.d(txt); } }
这样可以正常的和H5中js交互(本地、服务器一样),但是通过alert来调试数据时,一直没有作用。
通过查API,知道 有个setWebChromeClient的方法,官方解释为Sets the chrome handler. This is an implementation of WebChromeClient for use in handling JavaScript dialogs, favicons, titles, and the progress.This will replace the current handler. 大体意思是这个方法是来处理 javascript 方法中的对话框,收藏夹,标题和进度条之类的。
我所要用的alert刚好属于 dialogs 一类。
将代码中“*****”
webView.setWebChromeClient(new WebChromeClient());
就可以显示了。
相关文章推荐
- android星星评分自定义控件
- Android起航系列第三章:android app的兼容性
- 【原创】第一个iOS应用程序
- 【原创】iOS开发入门教程
- [iOS]抓包工具Charles简单使用
- android 初始开发小问题
- iOS实例变量属性参数的区别
- Object-C--->Swift之(一)可选类型
- iOS---如何获取手机的本地照片和相册
- android利用xmpp进行即时通信
- 014: class and objects > 多态
- Android中怎么用this
- 我的Android进阶之旅------>Android Activity的singleTask加载模式和onActivityResult方法之间的冲突
- NGUI研究之在Unity中使用贝塞尔曲线
- webView的用法
- Android应用开发中模拟按HOME键效果
- 一些Android大牛的博客链接收集
- iOS Size class
- cocos2d-x问题记录:只在回调函数中执行动作会导致终端
- android学习项目