(iii) Android Webview in Depth (medium)

Source: Internet
Author: User

based on a certain understanding and understanding of the WebView control, we can continue to study this control, this article mainly describes how to make WebView and progressdialog combination.

The WebView component supports loading Web pages directly and can be viewed as a browser, with the following steps:

1. Create a new XML layout file as follows:

<span style= "FONT-SIZE:18PX;" ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <webview        android:id= "@+id/webview"        android:layout_width= "match_parent"        android:layout_ height= "Match_parent"/></relativelayout></span>
2. Initialize the control and set the corresponding parameters in the main activity

<span style= "FONT-SIZE:18PX;" >package Com.happy.wbpd;import Android.app.activity;import Android.app.alertdialog;import Android.app.progressdialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.view.keyevent;import Android.webkit.webview;import Android.webkit.webviewclient;import Android.widget.progressbar;import Android.widget.toast;public class Mainactivity extends Activity {private WebView Webview;private alertdialog alertdialog;private progressdialog progressBar; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitWebView ();} /** * Initialize WebView */private void Initwebview () {WebView = (WebView) This.findviewbyid (r.id.webview);p Rogressbar = Progress Dialog.show (mainactivity.this, NULL, "Entering webpage, please later ..."); Webview.getsettings (). Setjavascriptenabled (True); Webview.loadurl ("http://www.baidu.com/"); alertdialog = new Alertdialog.builder (mainactivity.this). Create (); Webview.setwEbviewclient (New Webviewclient () {@Overridepublic void onpagefinished (WebView view, String URL) {if ( Progressbar.isshowing ()) {Progressbar.dismiss ();}} @Overridepublic void Onreceivederror (WebView view, int errorcode,string description, String failingurl) {Toast.maketext (Mainactivity.this, "page loading error! ", Toast.length_long); Alertdialog.settitle (" ERROR "); Alertdialog.setmessage (description); Alertdialog.setbutton (" Ok ", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog,int which) {}}); Alertdialog.show ();} @Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return true;});} @Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back && webview.c Angoback ()) {webview.goback (); return true;} Return Super.onkeydown (KeyCode, event);}} </span>
3. If you need to add access to the Internet in Androidmanifest.xml, you cannot display:
<uses-permission android:name= "Android.permission.INTERNET"/>

The logic is relatively simple, plus today is more busy, so the general introduction, the next article will say WebView and JavaScript call each other and the ListView join WebView to implement the drop-down refresh.

(iii) Android Webview in Depth (medium)

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.