http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/WebViewDemo/
I modified it, and added a simple comment, this example is not only the operation of JS, it is also a simple introduction to the Android browser,
These I will explain in the note one by one.
The JavaScript pop-up box has the following three kinds:
- alert ();
- Window.confirm ("is you srue?");
- Window.prompt ("Please input some word";,"This is Text");
Three kinds of dialog were captured in Webchromeclient, but unfortunately, there is no callback function to use,
Or, you can't get the result of a user clicking "OK" or "Cancel".
Personally, these methods are set up to be used to test some applications that involve HTML operations.
The following methods are also available in webchromeclient:
- Onprogresschanged (WebView view, int newprogress);
- Onreceivedicon (WebView view, Bitmap icon);
- Onreceivedtitle (WebView view, String title);
- Onrequestfocus (WebView view);
- Onclosewindow (WebView window);
- Onprogresschanged (WebView view, int newprogress)
The use of these methods will be described in a later blog post.
Please look at the focus of today's explanation
- Public class Webviewdemo extends Activity {
- private static final String Log_tag = "Webviewdemo";
- private WebView Mwebview;
- private TextView Mreusulttext;
- private Handler Mhandler = new Handler ();
- @Override
- public void OnCreate (Bundle icicle) {
- super.oncreate (icicle);
- Setcontentview (R.layout.main);
- //Get browser Components
- //webview is a simple browser.
- //android Browser source code exists in the Linux\android\package\apps\browser
- all operations are carried out around the WebView .
- Mwebview = (WebView) Findviewbyid (R.id.webview);
- Mreusulttext = (TextView) Findviewbyid (R.id.resulttext);
- //websettings Almost all of the browser settings are in this class
- WebSettings websettings = Mwebview.getsettings ();
- Websettings.setsavepassword (false);
- Websettings.setsaveformdata (false);
- Websettings.setjavascriptenabled (true);
- Websettings.setsupportzoom (false);
- Mwebview.setwebchromeclient (new Mywebchromeclient ());
- /*
- * Demojavascriptinterface class provides interface for JS calling Android server side
- * Android as the client interface of the Demojavascriptinterface class is called by JS
- * The specific method of invocation is defined in Demojavascriptinterface:
- * For example, clickonandroid in this instance
- */
- Mwebview.addjavascriptinterface (new Demojavascriptinterface (),"Androd");
- Mwebview.loadurl ("file:///android_asset/page.html");
- }
- final class Demojavascriptinterface {
- Demojavascriptinterface () {}
- /**
- * This method is called by the browser side
- */
- public void Clickonandroid () {
- Mhandler.post (new Runnable () {
- public Void Run () {
- //Call the Onjsandroid method in JS
- Mwebview.loadurl ("javascript:onjsandroid ()");
- }
- });
- }
- }
- /**
- * Inherit Webchromeclient class
- * Processing of JS popup time
- *
- */
- final class Mywebchromeclient extends Webchromeclient {
- /**
- * Handle Alert Popup box
- */
- @Override
- Public Boolean onjsalert (WebView view,string URL,
- String Message,jsresult result) {
- LOG.D (Log_tag,"Onjsalert:" +message ");
- Mreusulttext.settext ("Alert:" +message);
- //Simple package for alert
- New Alertdialog.builder (Webviewdemo. This).
- Settitle ("Alert"). Setmessage (Message). Setpositivebutton ("OK",
- New Dialoginterface.onclicklistener () {
- @Override
- public void OnClick (Dialoginterface arg0, int arg1) {
- //todo
- }
- }). Create (). Show ();
- Result.confirm ();
- return true;
- }
- /**
- * Processing Confirm popup Box
- */
- @Override
- Public Boolean onjsconfirm (WebView view, string URL, String message,
- Jsresult result) {
- LOG.D (Log_tag, "onjsconfirm:" +message);
- Mreusulttext.settext ("Confirm:" +message);
- Result.confirm ();
- return super.onjsconfirm (view, URL, message, result);
- }
- /**
- * Processing Prompt popup box
- */
- @Override
- Public Boolean onjsprompt (WebView view, string URL, String message,
- String DefaultValue, jspromptresult result) {
- LOG.D (Log_tag,"onjsprompt:" +message);
- Mreusulttext.settext ("Prompt input is:" +message);
- Result.confirm ();
- return super.onjsprompt (view, URL, message, message, result);
- }
- }
- }
Category: Android good text to the top of my collection this article Oakpip
Follow-10
Fans-40 + plus attention40(Please comment on the article) «Previous: Android-----webview Use
» Next post: Android-----relativelayout Properties
Android-----js and Android Intermodulation