Simple use summary of Android WebView controls

Source: Internet
Author: User

1. Open the Web page directly from the URL Web site display content: Loadurl ()
    • First, write a WebView control in the layout
<?xml version= "1.0" encoding= "Utf-8"?><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 "tools:context=" Com.example.ygd.jreduch09.WebViewActivity ">                    <button  android:id  = "@+id/bt"  android:layout_alignparentright< /span>= "true"  android:text  =" go to " android:layout_width  = android:layout_height  =" Wrap_ Content "/>     <edittext  android:id  = "@+id/et"  android:layout_toleftof  =< Span class= "Hljs-value" > "@+id/bt"  android:layout_width  =" match_parent " android:layout_height  =" wrap_content "/>     <WebViewandroid:id="@+id/wv"android:layout_below="@+id/et" android:layout_width="Match_parent"android:layout_height="Match_parent" >                                    </WebView></relativelayout>
    • Then set the WebView in the activity
 Public  class webviewactivity extends appcompatactivity {    PrivateWebView WV;PrivateEditText et;PrivateButton BT;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_web_view);        wv= (WebView) Findviewbyid (R.ID.WV);        et= (EditText) Findviewbyid (r.id.et);        bt= (Button) Findviewbyid (R.ID.BT); Bt.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {String url="http //"+et.gettext (). toString (); WebSettings settings=wv.getsettings ();//Get Websetting objectSettings.setjavascriptenabled (true);//Set whether JavaScript is supportedSettings.setsupportzoom (true);//Set whether zooming is supportedSettings.setdisplayzoomcontrols (true);//Set built-in zoom controlWv.setwebviewclient (NewWebviewclient ());//Using webviewclient as a clientWv.loadurl (URL);//Open a page}        }); }}
    • As follows:
2. Loading HTML code with WebView: Loaddatawithbaseurl ()
    • First, write a WebView control in the layout (IBID.)
    • It then connects the HTML code to a string, calling the method
 Public  class webviewhtmlactivity extends appcompatactivity {    PrivateWebView WV;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.activity_web_view_html);        wv= (WebView) Findviewbyid (R.ID.WV); StringBuilder sbd=NewStringBuilder ();//Use StringBuilder here as far as possibleSbd.append ("); Sbd.append ("); Sbd.append ("<body>"); Sbd.append ("); Sbd.append ("<ul>"); Sbd.append ("<li> unordered list ul tags </li>"); Sbd.append ("</ul>"); Sbd.append ("<ol>"); Sbd.append ("<li> Nested li tags </li>"); Sbd.append ("</ol>"); Sbd.append ("</body>"); Sbd.append ("); Wv.loaddatawithbaseurl (NULL, Sbd.tostring (),"Text/html","Utf-8",NULL); }}
    • The effect is as follows:
3.Android and JS call each other (while overriding webchromeclient () to display the loading progress)
    • Write an HTML file into the project's assets directory (no new in the location)

    • The HTML code is as follows:

<html><head>    <title>My first page</title>    <script>  function test(){var a=1;            var b=' Week ';        alert (b+a); }                        </script><body><H1>Big Headlines</H1><ul>    <li>How are you doing</li>    <li>World</li>    <li>Ha ha</li></ul><input type="button" onclick="Test ()" value="click My "></body></head></html>
    • The main function code is as follows:
 Public  class loadhtmlactivity extends appcompatactivity {    PrivateWebView WV;PrivateButton Bt1,bt2;PrivateProgressBar ProgressBar;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.activity_load_html);        wv= (WebView) Findviewbyid (R.ID.WV);        bt1= (Button) Findviewbyid (R.ID.BT1);        Bt2= (Button) Findviewbyid (R.ID.BT2);        Progressbar= (ProgressBar) Findviewbyid (R.ID.PB);        WebSettings settings=wv.getsettings (); Settings.setjavascriptenabled (true); Wv.setwebchromeclient (NewMyClient ()); Wv.setwebviewclient (NewWebviewclient ()); Wv.loadurl ("File:///android_asset/1.html");//asset don't add s//Wv.loadurl ("http://www.baidu.com");Bt1.setonclicklistener (NewView.onclicklistener () {//Invoke methods in HTML via the layout button            @Override             Public void OnClick(View v) {Wv.loadurl ("File:///android_asset/1.html"); Wv.loadurl ("Javascript:test ()");        }        }); Bt2.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v)                {progressbar.setvisibility (view.visible); Wv.loadurl ("Http://www.baidu.com");    }        }); } Public  class myclient extends webchromeclient{        @Override         Public void onprogresschanged(WebView view,intnewprogress) {Super. onprogresschanged (view, newprogress);if(newprogress== -) {progressbar.setvisibility (view.gone); }        }    }}

Simple use summary of Android WebView controls

Related Article

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.