This example mainly introduces the use of WebView,WebView of the details can refer to the Android development WebView detailed .
This example is mainly implemented: the browser can open the specified Web page, you can load the local page in the assets directory, and you can view the previous page by the fallback key.
Program run:
Program code:
Package Com.jph.simplebrowser;import Android.os.bundle;import Android.app.activity;import android.app.AlertDialog; Import Android.content.dialoginterface;import Android.content.dialoginterface.onclicklistener;import Android.view.keyevent;import Android.view.menu;import Android.webkit.websettings;import Android.webkit.WebView; Import android.webkit.webviewclient;/** * describe:</br> * Simple Web browser * This instance can open a specified Web page and can load a local page in the assets directory * @author JPH * date:2014.08.03 * */public class Simplebrowser extends Activity {WebView webview;//declare a WebView helper class WebSettings Wsetti NGS; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); webview= (WebView) Findviewbyid (R.id.webview);//Acquire WebSettings object Wsettings=webview.getsettings (); /Enable Touch zoom Wsettings.setbuiltinzoomcontrols (TRUE);//enable the Support window Meta tag (can realize double-click Scaling) Wsettings.setusewideviewport (true);// Load page wsettings.setloadwithoverviewmode in thumbnail mode (true);//enable JavaScript support wsettings.setjavascriptenabled (TRUE);//Set to receive various notifications and requests for webviewclient (load all links in WebView) webview.setwebviewclient (New Webviewclient ());// Set the default loaded page Webview.loadurl ("http://www.baidu.com/"), or load the HTML in the local assets directory, or the file on the network//webview.loadurl ("file:/// Android_asset/win8_demo/index.html ");} @Overridepublic boolean onKeyDown (int keycode, keyevent event) {//TODO auto-generated method Stubswitch (keycode) {case K EYEVENT.KEYCODE_BACK://handles the Return key event if (Webview.cangoback ()) {webview.goback ();//Let WebView fall back to the previous page return true;} else {//if WebView cannot rollback//prompt app to quit program New Alertdialog.builder (Simplebrowser.this). Settitle ("hint"). Setmessage ("OK to exit the browser?"). "). Setpositivebutton (" OK ", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {// TODO auto-generated Method StubSimpleBrowser.this.finish ();//Exit Program}}). Setnegativebutton ("Cancel", null). Show (); Break;case keyevent.keycode_search://when//webview.loadurl (""); break;default:break;} return false;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the ACTIOn bar if it is present.getmenuinflater (). Inflate (R.menu.simple_browser, menu); return true;}}
layout file: Main.xml
<webview xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:id=" @+id/webview " android:layout_width=" Match_parent " android: layout_height= "Match_parent" > </WebView>
Finally, don't forget to add access to the network to your program:
<!--add permissions to use the network--><uses-permission android:name= "Android.permission.INTERNET"/>