Android Custom WebView Browser _android

Source: Internet
Author: User

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.

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

Adding <WebView/> controls to a layout file

Get the WebView object in the activity

Invokes the Loadurl () method of the WebView object, parameters: string path

To add permissions to access a network Android.permission.INTERNET

Invokes the GetSettings () method of the WebView object and gets to the WebSettings setting object

Invokes the Setsupportzoom () method of the WebSettings object, setting support for scaling, parameters: Boolean

Invokes the Setbuiltinzoomcontrols () of the WebSettings object, setting the zoom control, Parameters: Boolean,

Invokes the Setwebviewclient () method of the WebView object, setting the client to prevent the link from opening the system browser, parameters: Webviewclient Object

Listen for the Back button and return to the previous interface

Rewrite the onkeydown () method of the activity, with 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 up, call the WebView object's CanGoBack () method

Call the WebView object's GoBack () method, page back

Set Menu key, rewrite Oncreateoptionsmenu () method, pass in Menu object

Call the Menu object's Addsubmenu () method, add menu, Parameters: Group ID, Entry ID, sort, title

Add refresh, back, forward

Monitor small Menu click event

Rewrite the onoptionsitemselected () method to pass in the MenuItem object

Switch to judge the MenuItem object's GetOrder (), corresponding to the order above

Page refresh, calling the WebView object's reload () method

Page back, first call the WebView object's CanGoBack () method to determine if you can rewind, call the GoBack () method back

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

Page loading

Get ProgressDialog object, new out, parameter: Context

Call the ProgressDialog object's Setmessage () method, Parameters: Text

Call the WebView object's Setwebchromeclient () method, Parameter: Webviewclient object,

Anonymous inner class inherits Webviewclient class, Overrides Onpagestarted () method and Onpagefinshed () method

Within the onpagestarted () method

Call ProgressDialog object's Show () method

Within the onpagefinshed () method

Invoke the dismiss () method of the ProgressDialog object

Package Com.tsh.mywebview;
Import android.app.Activity;
Import Android.app.ProgressDialog;
Import Android.graphics.Bitmap;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.Window;
Import android.webkit.WebSettings;
Import Android.webkit.WebView;
Import android.webkit.WebViewClient; public class Mainactivity extends activity {private WebView webview; private progressdialog PD; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_
TITLE);
Setcontentview (R.layout.activity_main);
Pd=new ProgressDialog (this);
Pd.setmessage ("Loading ...");
The simple setting of WebView webview= (WebView) Findviewbyid (r.id.wv_internet);
Webview.loadurl ("https://www.baidu.com");
WebSettings websettings=webview.getsettings ();
Websettings.setsupportzoom (TRUE);
Websettings.setbuiltinzoomcontrols (TRUE); Webview.setwebviewclient (New Webviewclient () {@Override public voID onpagestarted (webview view, String URL, Bitmap favicon) {pd.show ();} @Override public void onpagefinished (WebView vie
W, String URL) {Pd.dismiss ();}}); }//Back key @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode==keyevent.keycode_back&&
Webview.cangoback ()) {webview.goback (); return true;} return Super.onkeydown (KeyCode, event); }//Menu key @Override public boolean oncreateoptionsmenu (0, 0, 0, refresh); menu.add (0, 0, 1, "back"); menu.a
DD (0, 0, 2, "forward");
return Super.oncreateoptionsmenu (menu); //Menu Click event @Override Public boolean onoptionsitemselected (MenuItem item) {switch (Item.getorder ()) {case 0:webview.relo
AD ();
Break
Case 1:if (Webview.cangoback ()) {webview.goback ()};
Case 2:if (Webview.cangoforward ()) {Webview.goforward ()};
return super.onoptionsitemselected (item); }
}

About this article to introduce you to the Android custom WebView browser content to introduce so much, I hope to help you!

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.