WebView使用小结----提示框的使用Alert,Confirm,Prompt.
2017-03-08 23:26
429 查看
今天主要介绍Alert,Confirm,Prompt.这几种提示框
这里主要重写WebChromeClient的3个方法:
onJsAlert :警告框(WebView上alert无效,需要定制WebChromeClient处理弹出)
onJsPrompt : 提示框.
onJsConfirm : 确定框.
首先onJsAlert这是警告提示框特(返回的值没什么特别意义)
在html文件的使用也很简单
onJsConfirm 这是确定提示框(返回值true,false)
这里要说下 result.confirm();返回就是true而 result.cancel();返回的就是false。
onJsPrompt 提示框这里需要自定一个提示的布局文件,如下:prompt_dialog.xml
这里要说下 result.confirm(value);返回就是对应值而 result.cancel();返回的就是null。
这里再给出html(不过和上次写 的放在一起了,0.0不影响)
好了就这么多了
这里主要重写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>
好了就这么多了
相关文章推荐
- Android webview中定制js的alert,confirm和prompt对话框的方法 (处理webview 带网站地址的弹出框)
- android studio 将HTML5封装成APK && 设置WebView支持js的Alert,Confirm,Prompt函数的弹出提示框.
- android webview获取js中的alert、confirm、和prompt,以及获取其值
- (转)在Android的webview中定制js的alert,confirm和prompt对话框的方法
- Android WebView封装及定制js的alert,confirm和prompt对话框的方法
- iOS用WKWebView与JS交互获取系统图片及WKWebView的Alert,Confirm,TextInput的监听代理方法使用,屏蔽WebView的可选菜单
- 在Android的webview中定制js的alert,confirm和prompt对话框的方法
- 在Android的webview中定制js的alert,confirm和prompt对话框的方法
- 在Android的webview中定做js的alert,confirm和prompt对话框的方法
- 在Android的webview中定制js的alert,confirm和prompt对话框的方法
- iOS拦截webView的Alert与Confirm并让地址不显示,iOS隐藏(或重写)webView弹出提示的title,http:xxx.xx
- 在Android的webview中定制js的alert,confirm和prompt对话框的方法
- 在Android的webview中定制js的alert,confirm和prompt对话框的方法
- 在Android的webview中定制js的alert,confirm和prompt对话框的方法
- Android webview中定制js的alert,confirm和prompt对话框的方法 (处理webview 带网站地址的弹出框)
- iOS用WKWebView与JS交互获取系统图片及WKWebView的Alert,Confirm,TextInput的监听代理方法使用,屏蔽WebView的可选菜单
- 在Android的webview中定制js的alert,confirm和prompt对话框的方法
- 如何设置WebView支持js的Alert,Confirm,Prompt函数的弹出提示框.
- 在Android的webview中定制js的alert,confirm和prompt对话框的方法
- 在Android的webview中定做js的alert,confirm和prompt对话框的方法