[Android] WebView a custom browser

Source: Internet
Author: User

Add <EditText/> and <Button/> controls to the layout file,

Adding <WebView/> controls to a layout file

get the WebView object in Activity

Call the Loadurl () method of the WebView object , Parameter:String Path

Add permission to access the network Android.permission.INTERNET

Call the getsettings () method of the WebView object to get to the websettings Settings Object

Call the setsupportzoom () method of the WebSettings object , set support scaling, Parameters: Boolean

Call the websettings object's setbuiltinzoomcontrols (), set the zoom control, Parameters: Boolean,

Call the setwebviewclient () method of the WebView object to set the client to prevent the link from opening the system browser, parameters:webviewclient Object

Listen Back button to return to the previous interface

Rewrite the Activity of the OnKeyDown () method, parameters passed in int Keyboard code,keyevent Object

If the keyboard code equals keyevent.keycode_back and the current WebView object has many pages to back, call the WebView object's cangoback () method

Call the GoBack () method of the WebView Object , and the page backs up

Set Menu key, override Oncreateoptionsmenu () method, pass in menu Object

Call the Menu object's addsubmenu () method, add menus, parameters: Group ID, entry ID, sort , the title

Add refresh, rewind, forward

Monitor small Menu click events

Override the onoptionsitemselected () method, passing in the MenuItem Object

Switch to determine the MenuItem object's getorder (), corresponding to the above sort

Page refresh, calling the reload () method of the WebView Object

Page back, first call the WebView object's cangoback () method, determine whether you can back, call the goBack () method back

Page forward, call the WebView object's cangoforward () method, determine whether it can advance, call GoForward () method Forward

Page loading

Get progressdialog object,new out, Parameters: Context

Call the setmessage () method of the progressdialog object , parameter: text

Call the setwebchromeclient () method of the WebView object, parameter:webviewclient object,

Anonymous inner classes inherit the webviewclient class, overriding the onpagestarted () method and the onpagefinshed () method

Within the onpagestarted () method

Call the Show () method of the progressdialog Object

Within the onpagefinshed () method

Call the dismiss () method of the progressdialog Object

 PackageCom.tsh.mywebview;Importandroid.app.Activity;ImportAndroid.app.ProgressDialog;ImportAndroid.graphics.Bitmap;ImportAndroid.os.Bundle;Importandroid.view.KeyEvent;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.Window;Importandroid.webkit.WebSettings;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient; Public classMainactivityextendsActivity {PrivateWebView WebView; PrivateProgressDialog PD; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);                Setcontentview (R.layout.activity_main); PD=NewProgressDialog ( This); Pd.setmessage ("Loading ..."); //simple settings for WebViewwebview=(WebView) Findviewbyid (r.id.wv_internet); Webview.loadurl ("Https://www.baidu.com"); WebSettings websettings=webview.getsettings (); Websettings.setsupportzoom (true); Websettings.setbuiltinzoomcontrols (true); Webview.setwebviewclient (Newwebviewclient () {@Override Public voidonpagestarted (WebView view, String URL, Bitmap favicon) {pd.show (); } @Override Public voidonpagefinished (WebView view, String URL) {Pd.dismiss ();            }        }); }    //back Key@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        if(keycode==keyevent.keycode_back&&Webview.cangoback ())            {Webview.goback (); return true; }        return Super. OnKeyDown (KeyCode, event); }    //Menu Key@Override Public BooleanOncreateoptionsmenu (Menu menu) {menu.add (0, 0, 0, "refresh"); Menu.add (0, 0, 1, "Back"); Menu.add (0, 0, 2, "forward"); return Super. Oncreateoptionsmenu (menu); }    //Menu click events@Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getorder ()) { Case0: Webview.reload ();  Break;  Case1:            if(Webview.cangoback ()) {webview.goback (); }             Break;  Case2:            if(Webview.cangoforward ()) {Webview.goforward (); }             Break; }        return Super. onoptionsitemselected (item); }}

[Android] WebView a custom browser

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.