Activity configuration file: Activity_main.xml
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal" >
<edittext
Android:id= "@+id/et_address"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "0.5"
android:text= "Http://www.baidu.com"/>
<button
Android:id= "@+id/search"
Android:layout_width= "101DP"
android:layout_height= "Match_parent"
android:onclick= "click"
android:text= "Search"/>
</LinearLayout>
<webview
Android:id= "@+id/mywebview"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "0.08"/>
</LinearLayout>
Mainactivity.java
PackageCom.example.mybrowser;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.Window;Importandroid.webkit.WebSettings;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient;ImportAndroid.widget.EditText; Public classMainactivityextendsActivity {Private Static FinalString URL = "http://www.baidu.com"; PrivateEditText et_address; PrivateWebView Mywebview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Et_address=(EditText) Findviewbyid (r.id.et_address); Mywebview=(WebView) Findviewbyid (R.id.mywebview); WebSettings mysettings=mywebview.getsettings (); Mysettings.setsupportzoom (true); Mysettings.setbuiltinzoomcontrols (true); Mywebview.setwebviewclient (Newwebviewclient ()); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present. //getmenuinflater (). Inflate (R.menu.main, menu);Menu.addsubmenu (0,0,0, "Refresh"); Menu.addsubmenu (0,0,1, "forward"); Menu.addsubmenu (0,0,2, "back."); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//TODO auto-generated Method Stub Switch(Item.getorder ()) { Case0: Mywebview.reload (); Break; Case1: if(Mywebview.cangoback ()) {mywebview.goback (); } Else { } Break; Case2: if(Mywebview.cangoforward ()) {Mywebview.goforward (); } Break; } return true; } Public voidClick (View v) {String URL=et_address. GetText (). toString (). Trim (); if(url = =NULL||Url.isempty ()) {URL=URL; } mywebview.loadurl (URL); }}
Remember to include access to the network in the manifest configuration file:
<uses-permission android:name= "Android.permission.INTERNET"/>
As follows:
Android WebView Simple to use demo