This example describes the Android WebView component usage. Share to everyone for your reference, specific as follows:
If you want WebView to be able to access the network, you must add permissions within Androidmanifest.xml
<uses-permission android:name= "Android.permission.INTERNET"/>
Main.xml is very simple, is a webview
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
android:o" rientation= "vertical" >
<webview
android:id= "@+id/webview" android:layout_width= "Fill_parent"
android:layout_height= "fill_parent"/>
</LinearLayout>
Webviewdemoactivity.java Code:
Package COM.TIANJF;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.webkit.WebView;
public class Webviewdemoactivity extends activity {
private webview Mwebview;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Mwebview = (webview) Findviewbyid (R.id.webview);
Get WebSettings object, set support JavaScript parameters
//If JavaScript is available on the page you are accessing, WebView must be set to support JavaScript, otherwise a blank page
will be displayed Mwebview.getsettings (). Setjavascriptenabled (true);
Load URL
mwebview.loadurl ("http://www.baidu.com/");
}
When you run it, you will see that the URL is properly load out.
But there is a problem, when click on the link to continue browsing, it will pop-up system default browser, in order to be able to continue browsing in WebView, to use the Shouldoverrideurlloading method:
@Override public
void OnCreate (Bundle savedinstancestate) {.............
.
//Although the Google homepage is displayed in WebView, if you click on the link to continue browsing, it will be displayed to the system default browser//to
continue displaying in WebView, You need to rewrite the Shouldoverrideurlloading method
mwebview.setwebviewclient (New Mywebviewclient ());
...........
}
Private class Mywebviewclient extends Webviewclient {
@Override public
boolean shouldoverrideurlloading ( WebView view, String URL) {
view.loadurl (URL);
return true;
}
Also, if you want to return to the previous page by pressing the rollback key,
/**
* Press the back key to return to the previous page * *
@Override public
boolean onKeyDown (int keycode, keyevent event) {
if ((keycode = = keyevent.keycode_back) && mwebview.cangoback ()) {
mwebview.goback ();
return true;
}
Return Super.onkeydown (KeyCode, event);
}
For Android 2.0 There is a new way to start, for the activity can get the back key of the press event, directly rewrite the onbackpressed method, the code is as follows
@Override public
void onbackpressed () {
///Here handles the logical code, which applies only to 2.0 or later SDK return
;
If you want to load the HTML in your project, you can use the following method (provided HTML is placed in the assets directory)
Mwebview.loadurl ("file:///android_asset/test.html");
If you want to load an HTML text directly, you can use the following method
Here are WebView other uses:
Setting allow access to file data
Mwebview.getsettings (). Setallowfileaccess (True);
Setting support Scaling
Mwebview.getsettings (). Setbuiltinzoomcontrols (True);
Set whether to save passwords
Mwebview.getsettings (). Setsavepassword (false);
Setting up support for a variety of different devices
Copy Code code as follows:
Mwebview.getsettings () setuseragentstring ("mozilla/5.0" (IPad; U CPU os 3_2 like Mac os x;en-us) applewebkit/531.21.10 (khtml, like Gecko) version/4.0.4 mobile/7b334bsafari/531.21.10 ");
Some methods to perform when loading an WebView Web page
Mwebview.setwebviewclient (New Webviewclient () {
//when opening the page with its own definition of webview to display, without the system with the browser to display
@Override
public boolean shouldoverrideurlloading (webview view, String URL) {
view.loadurl (URL);
return true;
}
The work to do when you start loading a Web page
@Override public
void onpagestarted (webview view, String URL, Bitmap favicon) {
super.onpagestarted (view, URL, favicon);
The work to do when the load completes
@Override public
void onpagefinished (webview view, String URL) {
super.onpagefinished ( view, URL);
The work to do when loading errors
@Override public
void Onreceivederror (webview view, int errorcode,
String description, String failingurl) {
super.onreceivederror (view, errorcode, description, Failingurl);
}
);
Process some dialog box information in a Web page (prompt dialog, with selected dialog box, with Input dialog box)
Mwebview.setwebchromeclient (New Webchromeclient () {//prompt dialog box @Override public boolean Onjsalert (WebView view, String URL, String message, final Jsresult result) {//Build a Builder to display the Alert dialog box in the Web page Builder Builder =
New Builder (Webviewdemoactivity.this);
Builder.settitle ("Prompt dialog box");
Builder.setmessage (message); Builder.setpositivebutton (Android. R.string.ok, New Alertdialog.onclicklistener () {@Override public void OnClick (dialogint
Erface dialog, int which) {result.confirm ();
}
});
Builder.setcancelable (FALSE);
Builder.create ();
Builder.show ();
return true; ///Button Dialog @Override public boolean onjsconfirm (WebView view, string URL, String message, final Jsre
Sult result) {Builder Builder = new Builder (webviewdemoactivity.this);
Builder.settitle ("dialog box with Selection");
Builder.setmessage (message); BuIlder.setpositivebutton (Android. R.string.ok, New Alertdialog.onclicklistener () {@Override public void OnClick (dialogint
Erface dialog, int which) {result.confirm ();
}
}); Builder.setneutralbutton (Android. R.string.cancel, New Alertdialog.onclicklistener () {@Override public void OnClick (Dialo
Ginterface dialog, int which) {result.cancel ();
}
});
Builder.setcancelable (FALSE);
Builder.create ();
Builder.show ();
return true; dialog box with input box @Override public boolean onjsprompt (WebView view, string URL, String message, String def Aultvalue, final jspromptresult result) {Layoutinflater Inflater = Layoutinflater. from (Webviewdemoactivi
Ty.this);
Final View v = inflater.inflate (r.layout.mydialog, NULL); Sets the hint information ((TextView) v in the TextView corresponding Web page.Findviewbyid (R.id.textview)). SetText (message);
Sets the input box ((edittext) V.findviewbyid (R.id.edittext) in the EditText corresponding Web page. SetText (defaultvalue);
Builder Builder = new Builder (webviewdemoactivity.this);
Builder.settitle ("dialog box with input");
Builder.setview (v); Builder.setpositivebutton (Android. R.string.ok, New Alertdialog.onclicklistener () {@Override public void OnClick (dialogint Erface dialog, int which) {String value = (edittext) v. Findviewbyid (R.I
D.edittext)). GetText (). toString ();
Result.confirm (value);
}
}); Builder.setnegativebutton (Android. R.string.cancel, New Alertdialog.onclicklistener () {@Override public void OnClick (Dialo
Ginterface dialog, int which) {result.cancel ();
}
}); Builder.setoncancellistener (New DialogiNterface.
Oncancellistener () {@Override public void OnCancel (Dialoginterface dialog) {result.cancel ();
}
});
Builder.create ();
Builder.show ();
return true; //Set the Web page load progress bar @Override public void onprogresschanged (webview view, int newprogress) {Webviewdemoacti
Vity.this.getWindow (). Setfeatureint (window.feature_progress, newprogress * 100);
Super.onprogresschanged (view, newprogress); //Set the title of the application @Override public void Onreceivedtitle (WebView view, String title) {webviewdemoactivity.t
His.settitle (title);
Super.onreceivedtitle (view, title);
}
});
For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", "Android Basic Components Usage Summary", " Android View tips Summary, Android layout layout tips and a summary of Android controls usage
I hope this article will help you with the Android program.