Android browses web pages in a program
This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020
Sometimes you need to browse some web pages in the program. Of course, you can open the browser by calling the system, but in most cases, this method is not applicable.
The following describes how to browse the Web page in a program:
In fact, here we mainly use the WebView control and some of its methods.
The WebView loadUrl (String url) can be used to load the webpage content of the specified address and display it in the control. The previous and next pages correspond to the goBack () and goForward () functions of WebView respectively () method;
Layout file: main. xml
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;">
Main Code: WebViewTest
Public class WebViewTest extends Activity {private Button go; private EditText mEditText1; private WebView mWebView1; private Button bt_next, bt_previous;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); go = (Button) findViewById (R. id. bt_go); bt_next = (Button) findViewById (R. id. bt_next); bt_previous = (Button) findViewById (R. id. bt_previous); go. setOnClickListener (new ClickEvent (); bt_next.setOnClickListener (new ClickEvent (); bt_previus.setonclicklistener (new ClickEvent (); mEditText1 = (EditText) findViewById (R. id. myEditText); mEditText1.setText ("http://www.baidu.com"); mWebView1 = (WebView) findViewById (R. id. myWebView); mWebView1.setWebViewClient (new WebViewClien T () {@ Override public void onPageFinished (WebView view, String url) {// TODO Auto-generated method stub super. onPageFinished (view, url); // mEditText1.setText (url); Toast. makeText (WebViewTest. this, "loaded", Toast. LENGTH_SHORT ). show () ;}});} class ClickEvent implements OnClickListener {@ Override public void onClick (View v) {switch (v. getId () {case R. id. bt_go:/* set to capture the content in EditText */String strURI = (MEditText1.getText (). toString ());/*? Web page data */mWebView1.loadUrl (strURI); Toast. makeText (WebViewTest. this, "loading" + strURI, Toast. LENGTH_LONG ). show (); break; case R. id. bt_next: mWebView1.goForward (); // System. out. println (mWebView1.getUrl (); break; case R. id. bt_previous: mWebView1.goBack (); break ;}}}}
In the code, onPageFinished is triggered when the webpage is loaded. However, it indicates that the webpage framework is loaded and the image is not loaded.
Finally, do not forget to add the network access permission:
Now, you can browse the Web page in the program. You can enter the corresponding URL in EditText to browse various web pages. You can also implement the forward and backward functions of the web page through the previous and next pages, it is actually a simple browser.