This time I browsed the page to add a forward back, exit, and return to the home page feature.
On the code,
public class Mainactivity extends Activity implements View.onclicklistener {private EditText et_url; Private Button btn_visit; Private WebView web; Private Button Btn_home,btn_left,btn_right,btn_exit; Private Boolean isexit; Private String homestr = "http://www.baidu.com"; -------------------------------------------public void OnCreate (Bundle savedinstancestate) {super.oncreate (s Avedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.main); Init (); }//-------------------------------------------/** * Basic Data initialization */private void init () {isexit = Fal Se Et_url = (EditText) Findviewbyid (R.id.et_url); Et_url.settext (HOMESTR); Btn_visit = (Button) Findviewbyid (r.id.btn_visit); web = (WebView) Findviewbyid (R.id.web); WebSettings ws = Web.getsettings (); Whether scripts are allowed to support ws.setjavascriptenabled (true); Web.setwebviewclient (WVC); Btn_home = (Button) Findviewbyid (r.id.btn_home); Btn_left = (Button) Findviewbyid (r.id.btn_left); Btn_right = (Button) Findviewbyid (r.id.btn_right); Btn_exit = (Button) Findviewbyid (r.id.btn_exit); Btn_visit.setonclicklistener (this); Btn_home.setonclicklistener (this); Btn_left.setonclicklistener (this); Btn_right.setonclicklistener (this); Btn_exit.setonclicklistener (this); Conn (Et_url.gettext (). toString ()); }//-------------------------------------------webviewclient WVC = new Webviewclient () {public Boolean Shoul Doverrideurlloading (WebView view, String URL) {view.loadurl (URL); Et_url.settext (URL); return true; }; }; -------------------------------------------/** * Access URL * @param urlstr */private void conn (String ur lstr) {String url = ""; if (Urlstr.contains ("http://")) {url = urlstr; }else{URL = "HTtp://"+URLSTR; } web.loadurl (URL); }//-------------------------------------------/** * back */private void GoBack () {if (Web.cangobac K ()) {web.goback (); }else{Toast.maketext (This, "already the first page", Toast.length_short). Show (); }}//-------------------------------------------/** * forward * */private void GoForward () {if (web. CanGoForward ()) {Web.goforward (); }else{Toast.maketext (This, "already the last page", Toast.length_short). Show (); }}//-------------------------------------------/** * exit */private void exit () {Alertdialog. Builder builder = new Alertdialog.builder (this); Builder.settitle ("hint"). Setmessage ("OK exit?"). "). Setpositivebutton (" OK ", Dialoglistener). Setnegativebutton (" Cancel ", Dialoglistener) . Create (). Show (); } Dialoginterface.onclicklistener DialogliStener = new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog, int which) { Switch (which) {case DialogInterface.BUTTON_POSITIVE:finish (); Break Default:dialog.cancel (); Break } } }; -------------------------------------------public boolean onKeyDown (int keycode, keyevent event) {if (keycode ==keyevent.keycode_back| | Web.cangoback ()) {web.goback (); if (!web.cangoback ()) {if (isexit) {return Super.onkeydown (KeyCode, event); } Isexit = true; Toast.maketext (This, "Press again to exit", Toast.length_short). Show (); }else{isexit = false; }} return true; }//-------------------------------------------public void OnClick (View v) {switch (V.getid ()){Case R.id.btn_visit:conn (Et_url.gettext (). toString ()); Break Case R.id.btn_home:conn (HOMESTR); Break Case R.id.btn_left:goback (); Break Case R.id.btn_right:goforward (); Break Case R.id.btn_exit:exit (); Break } }}
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:orientation=" vertical "android:layout_width=" fill_parent "Android:layout_heigh t= "Fill_parent" > <scrollview android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "0DP" android:layout_weight= "1" > <linearlayout android:orientatio n= "vertical" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" > <linearlayout android:id= "@+id/layout" android:orientation= "Horizontal" an Droid:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_margin= "10DP" > <edittext android:id= "@+id/et_url" android:layout_width = "0DP" Android:layout_height= "Wrap_content" android:layout_weight= "1" android:inputtype= "Texturi" Android:singleline= "true" android:layout_margin= "10DP"/> <button Android:id= "@+id/btn_visit" android:layout_width= "Wrap_content" android:layou t_height= "Wrap_content" android:layout_margin= "10DP" android:text= "visit" Android:textsize= "15DP"/> </LinearLayout> <webview android:id= " @+id/web "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "/> </LinearLayout> </ScrollView> <linearlayout android:id= "@+id/ll_btn" Android:orientati on= "Horizontal" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" Android:gravi Ty= "Center_horizontal" > <button android:id= "@+id/btn_home" android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content" android:layout_weight= "1" android:text= "Home"/> <button Android:id= "@+id/btn_left" android:layout_width= "wrap_content" android:layout_height= "wrap_content "android:layout_weight=" 1 "android:text=" left "/> <button android:id=" @+id/b Tn_right "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Androi d:layout_weight= "1" android:text= "right"/> <button android:id= "@+id/btn_exit" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "Exit"/> </LinearLayout></LinearLayout>
<uses-permission android:name="android.permission.INTERNET"/>
Because this is actually from the foundation of the previous article added some UI design, no knowledge points, next time I do the web finger Touch Zoom small, please look forward to it
Good bye
Android Home Browser Two forward back return homepage exit