The WebView component itself is a browser implementation, and its kernel is based on WebKit
1. Browse the Web using WebView:
Common methods:
void GoBack (): Back
void GoForward (): Forward
void Loadurl (String URL): Loads the page that corresponds to the specified URL
void Loadurl ("file:///android_asset/XX.html"): Load local file
Boolean zoomin (): Enlarge Web page
Boolean zoomout (): Shrinking Web page
Demo:
Show Baidu Web page
Layout file:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" a ndroid:orientation= "vertical" > <linearlayout android:layout_width= "fill_parent" Android:layout_hei ght= "wrap_content" android:orientation= "horizontal" > <edittext android:id= "@+id/url" Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_weight= "1"/> <button android:id= "@+id/go" android:layout_width= "Wrap_content" Android oid:layout_height= "Wrap_content" android:text= "Access"/> </LinearLayout> <webview Android : id= "@+id/web" android:layout_width= "fill_parent" android:layout_height= "Fill_parent"/></LinearLayout
Activity code:
public class Mainactivity extends Activity {WebView show; EditText Urltext; Button Gobutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); show = (WebView) Findviewbyid (r.id.web); Show.getsettings (). Setjavascriptenabled (true); Gobutton = (Button) Findviewbyid (r.id.go); urltext = (EditText) Findviewbyid (R.id.url); Gobutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {String path = Urltext.gettext ( ). toString (); Show.loadurl (path);}});}}
2. Load the HTML code with WebView:
WebView provides a way to load the HTML code:
LoadData (String data, String MimeType, String encoding):
Data: Specifies the HTML code that needs to be loaded
MimeType: Specifies the MIME type of the HTML code, which can be specified as text/html for HTML code
Encoding: Specifies the character set used for HTML code encoding, such as GBK
HTML code:
Activity code:
public class Mainactivity extends Activity {WebView show; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); show = (WebView) Findviewbyid (R.id.web) ; StringBuilder sb = new StringBuilder () sb.append ("
3. Use JavaScript in WebView to call the Android method:
In order for JavaScript scripts in WebView to call the Android method, WebView provides a websettings tool class
Call the Setjavascriptenabled (true) in the WebView to make JavaScript calls to the Android method.
The Addjavascriptinterface (Object object, String name) method is responsible for exposing object objects to the name object in JavaScript
Setallowfileaccess (TRUE);//settings allow access to file data
Setbuiltinzoomcontrols (TRUE);//settings support scaling
Setsavepassword (FALSE); Set whether to save password
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 ");// supports a variety of different devices
Steps:
1. Call WebView associated websettings setjavascriptenabled (true) to enable JavaScript invoke functionality
2. Call the Addjavascriptinterface () method to expose object objects to JavaScript
3. Call the Android method from the name object you just exposed in the JavaScript script
Activity:
public class Jscallandroid extends Activity {WebView WebView; @Overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_js_call_android); WebView = (WebView) Findviewbyid (R.id.webview);//Testwebview.loadurl ("file:///android_asset/test.html") in the Assets folder; WebSettings websettings = Webview.getsettings (); websettings.setjavascriptenabled (true);// Exposing the MyObject object to a JavaScript script//So that JavaScript in the Test.html page can invoke MyObject's method via MyObj Webview.addjavascriptinterface (new MyObject (This), "MYOBJ");}}
MyObject:
public class MyObject {Context mcontext; MyObject (Context c) {mcontext = C;} The method will be exposed to the JavaScript script called public void Showtoast (String name) {toast.maketext (Mcontext, name + ", Hello!). ", Toast.length_long). Show ();} The method will be exposed to the JavaScript script called public void Showlist () {//Displays a normal list dialog box new Alertdialog.builder (Mcontext). Settitle ("book list"). SetIcon (R.drawable.ic_launcher). Setitems (new string[] {"Crazy java Handout", "Crazy Android Handout", "Lightweight Java EE Enterprise Application Combat"}, NULL). Setpositivebutton ("OK", null). Create (). Show ();}}
Android------webview beginner