WebView is a very useful component of Android that is based on the WebKit web rendering engine, like Safai and Chrome, and can easily display the software interface by loading HTML data. Here is a brief introduction to WebView to implement Android simple browser instance code.
Implementation of the browser to return to the home page exit input URL function
The notes are clear, and I don't say much.
The first is the layout file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <linearlayout android:orientation= " Horizontal "android:layout_width=" Fill_parent "android:layout_height=" wrap_content "> <EditText android:id=" @ +id/et_url "android:layout_width=" 320DP "android:layout_height=" wrap_content "/> <button android:id=" @+id/btn _login "android:layout_height=" wrap_content "android:layout_width=" wrap_content "android:text=" Login "/> </ Linearlayout> <webview android:layout_weight= "2" android:id= "@+id/webview" android:layout_width= "Fill_parent" "android:layout_height=" fill_parent "/> <linearlayout android:layout_weight=" 7.5 "android:layout_width=" fill
_parent "android:layout_height=" fill_parent "android:orientation=" horizontal "android:background=" #000000 "> <button android:id= "@+id/btn_back" android:layout_weight= "1"
android:layout_height= "Wrap_content" android:layout_width= "wrap_content" android:text= "←"/> <Button android: Id= "@+id/btn_menu" android:layout_weight= "1" "android:layout_height=" Wrap_content "android:layout_width=" Wrap_ Content "android:text=" Home "/> <button android:id=" @+id/btn_forward "android:layout_weight=" 1 "android:layout_h eight= "Wrap_content" android:layout_width= "wrap_content" android:text= "→"/> <button android:id= "@+id/btn_ Exit "android:layout_weight=" 1 "android:layout_height=" wrap_content "android:layout_width=" Wrap_content "Android: text= "Exit"/> </LinearLayout> </LinearLayout>
Mainactivity
Package Com.example.webview;
Import Android.os.Bundle;
Import android.app.Activity;
Import android.view.KeyEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.Window;
Import android.webkit.WebChromeClient;
Import android.webkit.WebSettings;
Import Android.webkit.WebView;
Import android.webkit.WebViewClient;
Import Android.widget.Button;
Import Android.widget.EditText;
public class Mainactivity extends activity implements Onclicklistener {private String URL = null;
Private WebView WebView;
Private EditText Et_url;
Private Button Btn_login;
Private Button Btn_back;
Private Button Btn_exit;
Private Button Btn_forward;
Private Button Btn_menu;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Window progress bar requestwindowfeature (window.feature_indeterminate_progress);
Setcontentview (R.layout.activity_main);
Setprogressbarindeterminate (TRUE); WebView = (webview) Findviewbyid (R. Id.webview);
Et_url = (edittext) Findviewbyid (R.id.et_url);
Btn_login = (Button) Findviewbyid (R.id.btn_login);
Btn_back = (Button) Findviewbyid (r.id.btn_back);
Btn_exit = (Button) Findviewbyid (r.id.btn_exit);
Btn_forward = (Button) Findviewbyid (R.id.btn_forward);
Btn_menu = (Button) Findviewbyid (R.id.btn_menu);
Add click on five buttons to monitor event Btn_login.setonclicklistener (this);
Btn_back.setonclicklistener (this);
Btn_exit.setonclicklistener (this);
Btn_forward.setonclicklistener (this);
Btn_menu.setonclicklistener (this); ///Btn_login trigger event clicked WebView Start reading URL protected void startreadurl (String URL) {//TODO auto-generated method Stub//
WebView Load Web resource webview.loadurl (URL); Overwrite WebView The default behavior of opening a Web page through a system or third party browser//If you call the system or a Third-party browser to open the Web page for False webview.setwebviewclient (new Webviewclient () {@ Override public boolean shouldoverrideurlloading (webview view, String URL) {//TODO auto-generated stub//WEBV
Iew Load Web resource view.loadurl (URL);
return true;
}
}); Enable JavaScript WebS supportettings settings = Webview.getsettings ();
Settings.setjavascriptenabled (TRUE);
The Web load page uses cache loading Settings.setcachemode (websettings.load_cache_else_network) preferentially; Hide Progress bar webview.setwebchromeclient (new Webchromeclient () {@Override public void Onprogresschange When a page is opened when the progress bar page is opened completely
D (webview view, int newprogress) {//TODO auto-generated Method Stub settitle ("This page has been loaded" + newprogress + "%");
if (newprogress = =) {Closeprogressbar ();
else {Openprogressbar (newprogress);
} super.onprogresschanged (view, newprogress);
}
}); //Open progress bar protected void Openprogressbar (int x) {//TODO auto-generated method stub Setprogressbarindeterminatevisib
Ility (TRUE);
Setprogress (x); ///Close progress bar protected void Closeprogressbar () {//TODO auto-generated method stub Setprogressbarindeterminatevisibilit
Y (false); }//Rewrite the physical key to return the key logic @Override public boolean onKeyDown (int keycode, keyevent event) {//TODO auto-generated method Stu b if (keycode = = Keyevent.keycode_back) {if (weBview.cangoback ()) {//Return to previous page webview.goback ();
return true;
else {//Quit program finish ();
} return Super.onkeydown (KeyCode, event); //Handling button Events @Override public void OnClick (View v) {//TODO auto-generated Method Stub switch (V.getid ()) {case R
. Id.btn_login:url = "http://" + et_url.gettext (). toString ();
url = Url.replace ("", "");
Startreadurl (URL);
Break
Case R.id.btn_back:if (Webview.cangoback ()) {webview.goback ();
else {finish ();
} break;
Case R.id.btn_forward:if (Webview.cangoforward ()) {Webview.goforward ();
} break;
Case R.id.btn_exit:finish ();
Break
Case R.id.btn_menu:startreadurl ("http://www.baidu.com");
Break }
}
}
Finally, do not forget to configure network access permissions in the Androidmanifest.xml file
The above content has introduced the WebView to realize the Android Simple browser instance code, hoped that has the help to everybody!