您的位置:首页 > 移动开发

WebView使用小结----提示框的使用Alert,Confirm,Prompt.

2017-03-08 23:26 429 查看
今天主要介绍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>


好了就这么多了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  webview 提示框
相关文章推荐