WebView is a very practical component in Android. Like Safai and Chrome, it is based on the Webkit web rendering engine, and can easily display the software interface by loading HTML data.
Add <EditText/> and <Button/> controls in the layout file,
Adding <WebView/> control in the layout file
Get WebView object in Activity
Call the loadUrl() method of the WebView object, parameter: String path
Add permissions to access the network
Call the getSettings() method of the WebView object to get the WebSettings setting object
Call the setSupportZoom() method of the WebSettings object, set support scaling, parameters: Boolean value
Call setBuiltInZoomControls() of the WebSettings object to set the scaling control, parameters: Boolean value,
Call the setWebViewClient() method of the WebView object, set the client to prevent links from opening the system browser, parameters: WebViewClient object
Listen to the back button and return to the previous interface
Rewrite the onKeyDown() method of Activity, pass the parameters in the int keyboard code, and the KeyEvent object
If the keyboard code is equal to KeyEvent.KEYCODE_BACK and the current WebView object has many pages that can be backed, call the canGoBack() method of the WebView object
Call the goBack() method of the WebView object, and the page backs
Set menu key, override the onCreateOptionsMenu() method, and pass in the Menu object
Call the addSubMenu() method of the Menu object, add menu, parameters: group id, entry id, sort, title
Add refresh, backward, forward
Listen to small menu click events
Rewrite the onOptionsItemSelected() method and pass it in the MenuItem object
Switch determines the getOrder() of MenuItem object, corresponding to the above sort
Page refresh, call the reload() method of the WebView object
The page backs, first call the canGoBack() method of the WebView object to determine whether it can backs, and call the goBack() method to backs
The page is advanced, call the canGoForward() method of the WebView object to determine whether it can be advanced, call the goForward() method to advance
Page loading
Get the ProgressDialog object, new out, parameters: context
Call the setMessage() method of the ProgressDialog object, parameter: text
Call the setWebChromeClient() method of the WebView object, parameters: WebViewClient object,
Anonymous internal class inherits the WebViewClient class and overrides the onPageStarted() method and onPageFinshed() method.
In the onPageStarted() method
Call the show() method of the ProgressDialog object
In the onPageFinshed() method
Call the dismiss() method of the ProgressDialog object
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private WebView webview; private ProgressDialog pd; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(.activity_main); pd=new ProgressDialog(this); ("Loading..."); //Simple settings of webviewwebview=(WebView) findViewById(.wv_internet); (""); WebSettings websettings=(); (true); (true); (new WebViewClient(){ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { (); } @Override public void onPageFinished(WebView view, String url) { (); } }); } //Back key@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK&&()){ (); return true; } return (keyCode, event); } //Menu Key@Override public boolean onCreateOptionsMenu(Menu menu) { (0, 0, 0, "refresh"); (0, 0, 1, "Back"); (0, 0, 2, "go ahead"); return (menu); } //Menu Click Event@Override public boolean onOptionsItemSelected(MenuItem item) { switch (()) { case 0: (); break; case 1: if(()){ (); } break; case 2: if(()){ (); } break; } return (item); } }
I will introduce so much to you about the content of the Android custom WebView browser introduced in this article, and I hope it will be helpful to you!