Example analysis of WebView usage in Android _android

Source: Internet
Author: User

This example describes the WebView usage in Android. Share to everyone for your reference, specific as follows:

WebView is equivalent to a mini browser, using the WebKit kernel, so the perfect support html,javascript,css and so on.
In the development process should pay attention to several points:

Permission "Android.permission.INTERNET" must be used in 1.androidmanifest.xml, otherwise the Web page not available error will be created.
2. If you have JavaScript on the page you are visiting, WebView must set up to support JavaScript.

Copy Code code as follows:
Webview.getsettings (). Setjavascriptenabled (True);

3. If the link in the page, if you want to click on the link to continue to respond in the current browser, rather than the new Android system browser should be linked, you must overwrite the WebView Webviewclient object.

Mwebview.setwebviewclient (New Webviewclient () {public
 Boolean shouldoverrideurlloading (webview view, String URL) {
 view.loadurl (URL);
 return true;
 }
});

4. If you do not do any processing, browsing the Web, click the system "back" key, the entire browser will call finish () and end itself, if you want to browse the Web page back rather than the launch of the browser, you need to deal with the current activity and consumption of this back event.

public boolean onKeyDown (int keycode, keyevent event) {
 if (keycode = = keyevent.keycode_back) && mwebview.ca Ngoback ()) {
  mwebview.goback ();
   return true;
 }
 Return Super.onkeydown (KeyCode, event);
}

Instance:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background=" @drawable/bg_ Main "> <relativelayout android:id=" @+id/title "android:layout_width=" Fill_parent "android:layout_height=" 48DP "android:layout_alignparenttop=" true "android:background=" @drawable/bg_title "android:padding=" 0DP "> < TextView android:id= "@+id/news" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:l Ayout_centerinparent= "true" android:text= "Sina Weibo" android:textsize= "22SP" android:textcolor= "#ffffff"/> </rela tivelayout> <webview android:id= "@+id/web_view_pethome" android:layout_width= "Fill_parent" Android:layout_
 height= "Wrap_content" android:layout_below= "@+id/title"/> <progressbar android:id= "@+id/progress_bar" Android:layout_width= "wrap_content" android:layout_height= "wrap_content "android:layout_centerinparent=" true "android:visibility=" Gone "/> </RelativeLayout> 

Java file code:

private void Findviews () {Mwebview = (WebView) Findviewbyid (r.id.web_view_pethome);
Mprogressbar = (ProgressBar) Findviewbyid (R.id.progress_bar);
 private void Setwebview () {//Call Loadurl () method to load content Mwebview.loadurl (URL);
 Set the properties of the WebView, you can then execute the JavaScript script mwebview.getsettings (). Setjavascriptenabled (True);
 Sets the Zoom button Mwebview.getsettings (). Setbuiltinzoomcontrols (True);
 Mwebview.getsettings (). Setsupportzoom (True); Mwebview.setwebviewclient (New Webviewclient () {public Boolean shouldoverrideurlloading (webview view, String URL) {vie
  W.loadurl (URL);
 return true;
  @Override public void onpagefinished (webview view, String URL) {log.v ("WebView", "========onpagefinished=======");
  super.onpagefinished (view, URL);
 Mprogressbar.setvisibility (View.gone); @Override public void onpagestarted (webview view, String URL, Bitmap favicon) {log.v ("WebView", "========onpagestar")
  ted======= ");
  super.onpagestarted (view, URL, favicon); Mprogressbar.setvisibility (View.visibLE);
}
 }); /** * Return key listener Event/@Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_b
 ACK)) {if (Mwebview.cangoback ()) {mwebview.goback ());
 return true;
Return Super.onkeydown (KeyCode, event); }

Operation Effect:

WebView Refresh the current page:

Copy Code code as follows:
Mwebview.reload ();

I hope this article will help you with the Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.