Android WebView Simple App

Source: Internet
Author: User

WebView is a subclass of view that lets you display Web pages in activity. Pull a webview in the layout, a button, a edittext
The layout code is as follows:

<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"    Android:paddingbottom="@dimen/activity_vertical_margin"    Android:paddingleft="@dimen/activity_horizontal_margin"    Android:paddingright="@dimen/activity_horizontal_margin"    Android:paddingtop="@dimen/activity_vertical_margin"    Tools:context="Com.shendan.webrowser.MainActivity">    <edittext  android:id  = "@+id/edittext1"  android:layout_width  = "wrap_content"  android:layout_height  = "wrap_content"  android:layout_ Alignparentleft  = "true"  android:layout_ Alignparenttop  = "true"  android:ems  =< Span class= "Hljs-value" "Ten" />     <button  android:id  = "@+id/button1"  android:layout_width  = "wrap_content"  android:layout_height  = "wrap_content"  android:layout_alignbottom  = "@+id/edittext1"  android:layout_alignparentright  =         "true"  android:text  = "GO" />     <WebViewandroid:id= "@+id/webview1"android:layout_width="Match_ Parent "android:layout_height="match_parent "android:layout_alignleft=" @+id/ EditText1 "android:layout_below=" @+id/edittext1 " />                                        </relativelayout>

The source code is as follows:

 PackageCom.shendan.webrowser;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.KeyEvent;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.webkit.WebSettings;ImportAndroid.webkit.WebView;ImportAndroid.webkit.WebViewClient;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public  class mainactivity extends actionbaractivity {EditText EditText;    Button button; WebView WebView;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        EditText = (editText) Findviewbyid (R.ID.EDITTEXT1); Edittext.settext ("Http://www.baidu.com");        Button = (button) Findviewbyid (R.id.button1);        WebView = (WebView) Findviewbyid (R.ID.WEBVIEW1);        WebSettings websettings = Webview.getsettings (); Websettings.setjavascriptenabled (true); Websettings.setdomstorageenabled (true);//Click on the link in the page to open it directlyWebview.setwebviewclient (NewWebviewclient () { Public Boolean shouldoverrideurlloading(WebView view, String URL) {view.loadurl (URL);return true;        }               }); Button.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {//TODO auto-generated method stubString urlstr = Edittext.gettext (). toString ();            Webview.loadurl (URLSTR);    }        }); }//Click the Back button to return to the previous page     Public Boolean OnKeyDown(intKeycode,keyevent event) {if((keycode = = keyevent.keycode_back) && webview.cangoback ()) {Webview.goback ();return true; }return Super. OnKeyDown (KeyCode, event); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }@Override     Public Boolean onoptionsitemselected(MenuItem Item) {//Handle Action Bar item clicks here. The Action Bar would        //automatically handle clicks on the Home/up button, so long        //As you specify a parent activity in Androidmanifest.xml.        intid = item.getitemid ();if(id = = r.id.action_settings) {return true; }return Super. onoptionsitemselected (item); }}

There are a few points to note:
1, the website must include HTTP//,
2. To increase network permissions

<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

This makes it easy to browse the Web.

WebView API's reasonable call, you can achieve a more complete browser function
For example, open the page:

mywebview.loadurl ( "http.// www.google.com ""), Mywebview.loadurl (); The span class= "hljs-comment" >//html file is saved in advance to the asset folder.  string  htmlstring =  "; //load this HTML page.  Mywebview.loaddata (htmlstring, , ); //if there is Chinese, use.  Mywebview.loaddatawithbaseurl (null , htmlstring,  " Text/html ", " Utf-8 ", null );  

When your webview overwrite the URL-loaded behavior, it automatically accumulates a history of the pages visited, and you can use the GoBack () and GoForward () methods to move forward or backward through the history.

By overriding Webviewclient, you can better control the behavior within the page.

     Public  class mywebviewclient extends webviewclient {        @Override         Public void onpagefinished(WebView view, String URL) {//TODO auto-generated method stub            Super. onpagefinished (view, URL); }@Override         Public void onpagestarted(WebView view, String URL, Bitmap favicon) {//TODO auto-generated method stub            Super. onpagestarted (view, URL, favicon); }@Override         Public void onscalechanged(WebView view,floatOldscale,floatNewscale) {//TODO auto-generated method stub            Super. onscalechanged (view, Oldscale, Newscale); }@Override         Public Boolean shouldoverridekeyevent(WebView view, KeyEvent event) {//TODO auto-generated method stub            return Super. Shouldoverridekeyevent (view, event); }@Override         Public Boolean shouldoverrideurlloading(WebView view, String URL) {//TODO auto-generated method stub            return Super. shouldoverrideurlloading (view, URL); }    }

The next point is more important, is webview and JS interaction.

Android WebView Simple App

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.