WebView使用小结----提示框的使用Alert,Confirm,Prompt.
来源:互联网 发布:术大师淘宝论坛 编辑:程序博客网 时间:2024/05/20 23:56
今天主要介绍Alert,Confirm,Prompt.这几种提示框
这里主要重写WebChromeClient的3个方法:
onJsAlert :警告框(WebView上alert无效,需要定制WebChromeClient处理弹出)
onJsPrompt : 提示框.
onJsConfirm : 确定框.
首先onJsAlert这是警告提示框特(返回的值没什么特别意义)
//设置响应js 的Alert()函数 webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this); b.setTitle("Alert"); b.setMessage(message); b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); b.setCancelable(false); b.create().show(); return true; }
在html文件的使用也很简单
function onAlert(){ alert("This is a alert sample from html"); }
onJsConfirm 这是确定提示框(返回值true,false)
//设置响应js 的Confirm()函数 @Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this); b.setTitle("Confirm"); b.setMessage(message); b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.cancel(); } }); b.create().show(); return true; }
这里要说下 result.confirm();返回就是true而 result.cancel();返回的就是false。
onJsPrompt 提示框这里需要自定一个提示的布局文件,如下:prompt_dialog.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/prompt_message_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/prompt_input_field" android:layout_width="match_parent" android:layout_height="wrap_content" android:minWidth="250dp" android:selectAllOnFocus="true" android:scrollHorizontally="true"/></LinearLayout>
//设置响应js 的Prompt()函数 @Override public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) { final View v = View.inflate(MainActivity.this, R.layout.prompt_dialog, null); ((TextView) v.findViewById(R.id.prompt_message_text)).setText(message); ((EditText) v.findViewById(R.id.prompt_input_field)).setText(defaultValue); AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this); b.setTitle("Prompt"); b.setView(v); b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String value = ((EditText) v.findViewById(R.id.prompt_input_field)).getText().toString(); result.confirm(value); } }); b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.cancel(); } }); b.create().show(); return true; }
这里要说下 result.confirm(value);返回就是对应值而 result.cancel();返回的就是null。
这里再给出html(不过和上次写 的放在一起了,0.0不影响)
<!DOCTYPE html><html><head> <title></title> <meta charset="utf-8" /> <script type="text/javascript"> function showToast(toast) { control.showToast(toast); alert(control.showToast(toast)); } function log(msg) { console.log(msg); } //警告 function onAlert(){ alert("This is a alert sample from html"); } //确定 function onConfirm(){ var b = confirm("are you sure to login?"); alert("your choice is "+b); } //提示 function onPrompt(){ var b = prompt("please input your password","aaa"); alert("your input is "+b); } </script> <style type="text/css"> input{ width: 100px; height: 100px; margin: 20px; } </style></head><body> <input type="button" value="toast" onclick="showToast('hello world!这是我的显示')" > <input type="button" value="msg1" onclick="log('hello world!')" > <input type="text" value="2123131" > <input type="button" value="alert" onclick="onAlert()"/></br> <input type="button" value="confirm" onclick="onConfirm()"/></br> <input type="button" value="prompt" onclick="onPrompt()"/></br></body></html>
好了就这么多了
0 0
- WebView使用小结----提示框的使用Alert,Confirm,Prompt.
- alert、confirm、prompt的区别及使用
- 如何设置WebView支持js的Alert,Confirm,Prompt函数的弹出提示框.
- JavaScript 中三种常见的提示框(alert、confirm、prompt)
- JS的三种消息提示框alert confirm prompt
- android studio 将HTML5封装成APK && 设置WebView支持js的Alert,Confirm,Prompt函数的弹出提示框.
- alert,confirm,prompt的用法
- ExtJs--02--MessageBox相关弹窗alert,prompt,confirm使用
- Javascript中的window对象基本属性和方法以及对alert,confirm,prompt的使用
- JavaScript的三种消息框alert,confirm,prompt
- JavaScript的三种消息框alert,confirm,prompt
- JavaScript的三种弹窗消息框:alert+prompt+confirm 结合代码
- JavaScript的三种消息框alert,confirm,prompt
- JavaScript的三种消息框alert,confirm,prompt
- 【原创】使用blockUI制作自定义的漂亮的网页提示框(代替confirm和alert)
- 【原创】使用blockUI制作自定义的漂亮的网页提示框(代替confirm和alert)
- 使用blockUI制作自定义的漂亮的网页提示框(代替confirm和alert)
- 在Android的webview中定制js的alert,confirm和prompt对话框的方法
- leetcode_75. Sort Colors
- PTA 最长连续递增子序列
- Hibernate中使用Criteria查询及注解——(Dept.java)
- 欢迎使用CSDN-markdown编辑器
- 【3.8】
- WebView使用小结----提示框的使用Alert,Confirm,Prompt.
- Linux 管道pipe
- Mysql数据库常用分库和分表方式
- 导入导出excel文件
- EasyDarwin云平台部署实践
- js 各种高宽问题,浏览器高宽问题
- The Linux Programming Interface 09 Process Credentials 进程凭证
- AsyncTask简单使用
- Cesium原理篇:3D Tiles(1)渲染调度