Android WebView Display Web page

Source: Internet
Author: User

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">        <includeLayout= "@layout/uzone_top_bar" />    <WebViewAndroid:id= "@+id/webview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" />    </LinearLayout>
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "40DP"android:gravity= "Center_vertical" >    <TextViewAndroid:id= "@+id/uzone_top_textview_title"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:layout_centervertical= "true"android:gravity= "Center"Android:text=""android:textsize= "18SP" />    <RelativelayoutAndroid:id= "@+id/uzone_top_relativelayout_cancel"Android:layout_width= "50DP"Android:layout_height= "Match_parent"Android:layout_alignparentleft= "true"android:layout_centervertical= "true"android:gravity= "Center" >        <ImageViewAndroid:id= "@+id/uzone_top_imageview_cancel"Android:layout_width= "20DP"Android:layout_height= "20DP"Android:layout_marginleft= "10DP"Android:paddingleft= "10DP"Android:paddingtop= "10DP" />    </Relativelayout>    <ImageViewAndroid:id= "@+id/uzone_top_imageview_line"Android:layout_width= "Wrap_content"Android:layout_height= "Fill_parent"Android:layout_marginright= "2DP"Android:layout_toleftof= "@+id/uzone_top_button_ok"android:visibility= "Gone" />    <ButtonAndroid:id= "@+id/uzone_top_button_ok"Android:layout_width= "70dip"Android:layout_height= "Match_parent"Android:layout_alignparentright= "true"android:layout_gravity= "Center"Android:text= "button"Android:textcolor= "@android: Color/white"android:textsize= "18SP"android:visibility= "Gone" /></Relativelayout>
 Public classWebbrowseractivityextendsactivity{/*** Ushequmobile Address*/     Public Static FinalString url_prefix = "http://10.10.9.51:8080/UshequMobile/";  Public Static FinalString useragent = "Haiersoft.webbrowser"; PrivateWebView WebView; PrivateTextView title; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.web_browser); //titletitle =(TextView) Findviewbyid (r.id.uzone_top_textview_title); //Back buttonRelativelayout back =(relativelayout) Findviewbyid (r.id.uzone_top_relativelayout_cancel); Back.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {finish ();                }        }); WebView=(WebView) Findviewbyid (R.id.webview);                Configwebview (); Intent Intent=getintent (); String URL= Intent.getstringextra ("url"); if(NULL!=URL)        {webview.loadurl (URL); }        Else{Toast.maketext ( This, "url is empty", Toast.length_short). Show (); }    }    Private voidConfigwebview () {websettings websettings=webview.getsettings ();Websettings.setsupportzoom (FALSE);//Set Not scalableWebsettings.setjavascriptenabled (true); Settings support JavaScript websettings.setuseragentstring (useragent);//Set value for Web service to determine access source
Webviewclient is to help WebView handle various notifications, request events, specifically including: Webview.setwebviewclient (Newwebviewclient () { Public voidonpagefinished (WebView view, String URL) {Super. onpagefinished (view, URL); } Public voidonpagestarted (WebView view, String URL, Bitmap favicon) {Super. onpagestarted (view, URL, favicon); } @Override Public voidOnreceivederror (WebView view,intErrorCode, string description, String failingurl) { Super. Onreceivederror (view, ErrorCode, description, Failingurl); Toast.maketext (Getapplicationcontext (), description, Toast.length_long). Show (); } });

Webchromeclient mainly deal with parsing, rendering web browsers and other things to do

Webchromeclient is an auxiliary webview processing JavaScript dialog box, website icon, website title, loading progress etc.

Webview.setwebchromeclient (Newwebchromeclient ()//tour {@Override Public BooleanOnjsalert (WebView view, string URL, string message, jsresult Result)//dialog box {//result.confirm ();//return true;                return Super. Onjsalert (view, URL, message, result); } @Override Public voidOnreceivedtitle (WebView view, String title)//title {Super. Onreceivedtitle (view, title); Webbrowseractivity. This. Title.settext (title);                }                    }); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); } @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event)//key Response {if((keycode = = Keyevent.keycode_back) &&Webview.cangoback ())             {Webview.goback (); return true; }                 return Super. OnKeyDown (KeyCode, event); } @Overrideprotected voidOnPause () {Super. OnPause (); } @Overrideprotected voidOnrestart () {Super. Onrestart (); } @Overrideprotected voidOnresume () {Super. Onresume (); }            }

Android WebView Display Web page

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.