Fortune Telling Collection - Comprehensive fortune-telling - How to set the popup prompt box that WebView supports js's reminder, confirmation and prompt functions?
How to set the popup prompt box that WebView supports js's reminder, confirmation and prompt functions?
Here are three ways to rewrite WebChromeClient:
OnJsAlert: warning box (the warning on WebView is invalid, and you need to customize WebChromeClient to pop it up).
OnJsPrompt: prompt box.
OnJsConfirm: OK box.
The effect diagram is as follows:
1. Alarm
Step 2: Tips
Step 3 confirm
Let's take a look at the js page code first:
& lthtml & gt
& lthead & gt
& ltmeta . example . chenys . webview demo;
Import android.app.activity;
Import android.app.alertdialog;
Import android.content.dialoginterface;
Import android.os.bundle;
Import android.view.view;
Import android.webkit.jspromptresult;
Import android.webkit.jsresult;
Import android.webkit.webchromeclient;
Import android.webkit.websettings;
Import android.webkit.webview;
Import android.widget.edittext;
Import android.widget.textview;
/**
* Created by mChenys on 20151119.
*/
Public class TestAlertActivity extends Activity {
@ Overlay
Protected void oncreate (bundle saved instancestate) (
super . oncreate(savedInstanceState);
WebView webView = new WebView (this);
setContentView(webView);
webview . request focus();
web settings settings = webview . get settings();
settings . setjavascript enabled(true); //Enable js support
//Set the Alert () function to respond to js.
webview . setwebchromeclient(new WebChromeClient(){
@ Overlay
Public boolean onJsAlert(WebView view, string url, string message, final JsResult) {
Alert dialog box. Generator b = new alarm dialog box. The builder (testaleractivity.this);
b . settitle(" Alert ");
B.setMessage;
B.setPositiveButton (Android. R.string.ok, create a new DialogInterface. OnClickListener() {
@ Overlay
public void onClick(dialog interface dialog,int which) {
result.confirm()。
}
});
b . setcancelable(false);
b.create()。 show();
Return true
}
//Set the Confirm () function in response to js.
@ Overlay
Public Boolean onjsconfirm (webview view, string url, string message, final JsResult) {
Alert dialog box. Generator b = new alarm dialog box. The builder (testaleractivity.this);
B.setTitle ("confirmation");
B.setMessage;
B.setPositiveButton (Android. R.string.ok, create a new DialogInterface. OnClickListener() {
@ Overlay
public void onClick(dialog interface dialog,int which) {
result.confirm()。
}
});
B.setNegativeButton (Android. R.string.cancel,new DialogInterface。 OnClickListener() {
@ Overlay
public void onClick(dialog interface dialog,int which) {
result . cancel();
}
});
b.create()。 show();
Return true
}
//Set Prompt () function to respond to js.
@ Overlay
Public boolean onjsprompt (webview view, string url, string message, string defaultValue, final jspromptsultresult) {
The final view v = view.infect (testalaractivity.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 (default value);
Alert dialog box. Generator b = new alarm dialog box. The builder (testaleractivity.this);
B.setTitle ("prompt");
b . set view(v);
B.setPositiveButton (Android. R.string.ok, create a new DialogInterface. OnClickListener() {
@ Overlay
public void onClick(dialog interface dialog,int which) {
String value = ((edittext) v.findviewbyid (r.id.prompt _ input _ field)). getText()。 toString();
Result confirmation (value);
}
});
B.setNegativeButton (Android. R.string.cancel,new DialogInterface。 OnClickListener() {
@ Overlay
public void onClick(dialog interface dialog,int which) {
result . cancel();
}
});
b.create()。 show();
Return true
}
});
webview . loadurl(" file:///Android _ asset/index 3 . html ");
}
}
There are two things to note:
1. To override onJsPrompt method, we need to customize a prompt layout file as follows: prompt_dialog.xml
Just a prompt TextView and an input text EditTex.
[html] view plain text
& lt? Xml version =" 1.0 "encoding ="utf-8"? & gt
& ltLinearLayout xmlns:android= " "
Android:layout _ width = " match _ parent "
Android:layout _ height = " match _ parent "
Android:orientation = " vertical " & gt;
& lt text view
Android:id = " @+id/prompt _ message _ text "
Android:layout _ width = " match _ parent "
Android:layout _ height = " wrap _ content "/& gt;
& lt edit text
Android:id = " @+id/prompt _ input _ field "
Android:layout _ width = " match _ parent "
Android:layout _ height = " wrap _ content "
android:minWidth="250dp "
android:selectAllOnFocus="true "
Android:scroll horizontally = " true "/& gt;
& lt/linear layout & gt;
2. If 2. WebView needs to support js, remember to add js support.
web settings settings = webview . get settings();
settings . setjavascript enabled(true);
- Previous article:If you read only one book, which one would you choose?
- Next article:One of the desperate marriages will die. What is a desperate marriage?
- Related articles
- Neighbors line up for fortune telling _ neighbors line up for fortune telling.
- Legend of the Sword and the Chivalrous Man 1 Introduction
- I think I lost money today. Is there a fortune teller to help me calculate?
- What does fortune-telling fairy class _ divination fairy class mean?
- An unjust composition
- I met a boy on soul. After a few days, he said that he liked me and was willing to come to me from Shanghai and give up things there.
- Palmistry analysis of three stripes of thumb
- What's the moral of dreaming about boilers?
- What does Fulu mean?
- Fortune teller in Hong Kong and Taiwan _ Fortune teller in Hong Kong and Taiwan