Android: webview implements a simple browser

Source: Internet
Author: User

Android: webview implements a simple browser

Enables the browser to return to the forward homepage and exit the URL entry function.

The comment is very clear.

First, layout files

 

<Linearlayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <linearlayout android: orientation = "horizontal" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <edittext android: id = "@ + id/et_url" android: layout_width = "320dp" android: layout_height = "wrap_content"> <button android: id = "@ + id/btn_login" android: layout_height = "wrap_content" android: layout_width = "wrap_content" android: text = "login"> <webview android: layout_weight = "2" android: id = "@ + id/webView" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <linearlayout android: layout_weight = "7.5" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "horizontal" android: background = "#000000"> </linearlayout> </webview> </button> <button android: id = "@ + id/btn_back" android: layout_weight = "1" android: layout_height = "wrap_content" android: layout_width = "wrap_content" android: text = "Courier"> </button> <button android: id = "@ + id/btn_menu" android: layout_weight = "1" android: layout_height = "wrap_content" android: layout_width = "wrap_content" android: text = "Homepage"> </button> <button android: id = "@ + id/btn_forward" android: layout_weight = "1" android: layout_height = "wrap_content" android: layout_width = "wrap_content" android: text = "→"> </button> <button android: id = "@ + id/btn_exit" android: layout_weight = "1" android: layout_height = "wrap_content" android: layout_width = "wrap_content" android: text = "exit"> </button> </edittext> </linearlayout>

MainActivity
Package com. example. webview; import android. OS. bundle; import android. app. activity; import android. view. keyEvent; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. webkit. webChromeClient; import android. webkit. webSettings; import android. webkit. webView; import android. webkit. webViewClient; import android. widget. button; import android. widget. editText; public class MainActivity extends Activity implements OnClickListener {private String url = null; private WebView webView; private EditText et_url; private Button btn_login; private Button btn_back; private Button btn_exit; private Button btn_forward; private Button btn_menu; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // Window progress bar requestWindowFeature (Window. FEATURE_INDETERMINATE_PROGRESS); setContentView (R. layout. activity_main); setProgressBarIndeterminate (true); webView = (WebView) findViewById (R. id. webView); et_url = (EditText) findViewById (R. id. et_url); btn_login = (Button) findViewById (R. id. btn_login); btn_back = (Button) findViewById (R. id. btn_back); btn_exit = (Button) findViewById (R. id. btn_exit); btn_forward = (Button) findViewById (R. id. btn_forward); btn_menu = (Button) findViewById (R. id. btn_menu); // Add the listener event listener (this); btn_back.setOnClickListener (this); listener (this); btn_forward.setOnClickListener (this); listener (this );} // After the btn_login trigger event is clicked, webView starts to read urlprotected void startReadUrl (String url) {// TODO Auto-generated method stub // webView loads the web Resource webView. loadUrl (url); // overwrite webView by default, the web page is opened through the system or a third-party browser. // webView is called if false. setWebViewClient (new WebViewClient () {@ Overridepublic boolean shouldOverrideUrlLoading (WebView view, String url) {// TODO Auto-generated method stub // webView loads the web resource view. loadUrl (url); return true ;}}); // enables support for javascriptWebSettings settings = webView. getSettings (); settings. setJavaScriptEnabled (true); // The cache is used to load settings on the web page. setCacheMode (WebSettings. LOAD_CACHE_ELSE_NETWORK); // when the page is opened, the progress bar webView is hidden when the page is fully opened. setWebChromeClient (new WebChromeClient () {@ Overridepublic void onProgressChanged (WebView view, int newProgress) {// TODO Auto-generated method stubsetTitle ("this page has been loaded" + newProgress + "%"); if (newProgress = 100) {closeProgressBar ();} else {openProgressBar (newProgress);} super. onProgressChanged (view, newProgress) ;}}); // open the progress bar protected void openProgressBar (int x) {// TODO Auto-generated method stubsetProgressBarIndeterminateVisibility (true ); setProgress (x);} // close the progress bar protected void closeProgressBar () {// TODO Auto-generated method stubsetProgressBarIndeterminateVisibility (false );} // rewrite the logic of the physical button return key @ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (keyCode = KeyEvent. KEYCODE_BACK) {if (webView. canGoBack () {// returns the webView on the previous page. goBack (); return true;} else {// exit program finish () ;}} return super. onKeyDown (keyCode, event);} // handle button events @ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. btn_login: url = "http: //" + et_url.getText (). toString (); url = url. replace ("", ""); startReadUrl (url); break; case R. id. btn_back: if (webView. canGoBack () {webView. goBack () ;}else {finish () ;}break; case R. id. btn_forward: if (webView. canGoForward () {webView. goForward ();} break; case R. id. btn_exit: finish (); break; case R. id. btn_menu: startReadUrl ("http://www.baidu.com"); break ;}}}
Do not forget to configure the network access permission in the AndroidManifest. xml file.
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

Related Article

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.