Android Custom WebView Browser

  • 2021-07-09 09:19:35
  • OfStack

WebView is a very practical component in Android, which, together with Safai and Chrome1, is based on Webkit web page rendering engine, and can conveniently display the software interface by loading HTML data.

Add in the layout file < EditText/ > And < Button/ > Control,

Add in the layout file < WebView/ > Control

Getting an WebView object in Activity

Invoke the loadUrl () method of the WebView object, argument: String path

Add permissions to the network android. permission. INTERNET

Call the getSettings () method of the WebView object to get the WebSettings setting object

Call the setSupportZoom () method of the WebSettings object, set the support for scaling, parameter: Boolean value

Call setBuiltInZoomControls () of the WebSettings object, set the scaling control, parameter: Boolean,

Call the setWebViewClient () method of the WebView object and set the client to prevent the link from opening the system browser. Parameter: WebViewClient object

Monitor the back key and return to the previous interface

Override the onKeyDown () method of Activity, pass in the int keyboard code and KeyEvent object as parameters

If the keyboard code is equal to KeyEvent. KEYCODE_BACK and the current WebView object has many pages that can be rolled back, call the canGoBack () method of the WebView object

Call the goBack () method of the WebView object, and the page falls back

Set the menu key, override the onCreateOptionsMenu () method, and pass in the Menu object

Invoke the addSubMenu () method of the Menu object, add menu, parameters: group id, entry id, sort, title

Add Refresh, Backward, Forward

Listen for small menu click events

Override the onOptionsItemSelected () method and pass in the MenuItem object

getOrder () of MenuItem object under switch judgment 1 corresponds to the above sort

Page refresh, calling reload () method of WebView object

Page backwards, first call canGoBack () method of WebView object, judge whether can backwards, call goBack () method backwards

Page forward, call canGoForward () method of WebView object, judge whether it can advance, call goForward () method to advance

Page loading

Get ProgressDialog object, new out, parameter: context

Invoke the setMessage () method of the ProgressDialog object, argument: text

Invoke the setWebChromeClient () method of the WebView object, argument: WebViewClient object,

The anonymous inner class inherits the WebViewClient class and overrides the onPageStarted () and onPageFinshed () methods

Within the onPageStarted () method

Call the show () method of the ProgressDialog object

Within the onPageFinshed () method

Call 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 ...");
//webview Simple settings of 
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 view, 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(Menu menu) {
menu.add(0, 0, 0, " Refresh ");
menu.add(0, 0, 1, " Backward ");
menu.add(0, 0, 2, " Advance ");
return super.onCreateOptionsMenu(menu);
}
// Menu click event 
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getOrder()) {
case 0:
webview.reload();
break;
case 1:
if(webview.canGoBack()){
webview.goBack();
}
break;
case 2:
if(webview.canGoForward()){
webview.goForward();
}
break;
}
return super.onOptionsItemSelected(item);
}
}

About the content of Android custom WebView browser introduced to you in this article, I hope it will be helpful to you!


Related articles: