This article is your own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020
Sometimes you need to browse some pages in your program, and of course you can open your browsing by invoking the browser of your system, but in most cases this does not apply.
Here's how to browse the Web in a program, first look at:
In fact, this is mainly the use of WebView control and some of its methods.
The WebView loadurl (String URL) can load the Web page content of the specified address and appear in the control, with the functions of the previous and next pages corresponding to WebView's GoBack () and GoForward () methods respectively;
Layout file: Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:background=" @drawable/white "android:layout_width=" fill_parent "android:layout_height=" Fill_ Parent "> <edittext android:id=" @+id/myedittext "android:layout_width=" 230DP "android:layout_height=" Wrap_co Ntent "android:layout_margin=" 10DP "/> <button android:id=" @+id/bt_go "android:layout_width=" Wrap_content "and roid:layout_height= "Wrap_content" android:text= "Go to" android:layout_torightof= "@id/myedittext" Android:layout_ margin= "10DP"/> <webview android:id= "@+id/mywebview" android:layout_width= "Match_parent" Android : layout_height= "match_parent" android:background= "@drawable/black" android:focusable= "false" android:layou t_below= "@id/bt_go" android:layout_margin= "10DP"/> <linearlayout android:layout_width= "Wrap_conten T "android:layout_height=" wrap_content "an"Droid:layout_alignparentbottom= "true" android:layout_centerhorizontal= "true" > <button Android Oid:id= "@+id/bt_previous" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Previous"/> <button android:id= "@+id/bt_next" Android:layout_width= "W Rap_content "android:layout_height=" Wrap_content "android:text=" Next "/> </linearla Yout> </RelativeLayout>
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_previous.setonclicklistener (New Clickevent ()); MEditText1 = (EditText) Findviewbyid (R.id.myedittext); Medittext1.settext ("http://www.baidu.com"); MWebView1 = (WebView) Findviewbyid (R.id.mywebview); Mwebview1.setwebviewclient (New Webviewclient () {@Override public void onpagefinished (WebView view, St Ring URL) {//TODO auto-generated method sTub super.onpagefinished (view, URL); Medittext1.settext (URL); Toast.maketext (Webviewtest.this, "Loading complete", Toast.length_short). Show (); } }); } class Clickevent implements onclicklistener{@Override public void OnClick (View v) {switch (V.getid ()) { Case R.ID.BT_GO:/* Sets the contents of the Fetch EditText */String Struri = (Medittext1.gettext (). toString ()); /*? WebView inside Display 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 } } }}
The code in the onpagefinished, can be literally understood to be when the page is loaded, but the end of the page frame loading, it is possible that the picture is not loaded end.
Finally, don't forget to add access to the network:
At this point, you can browse the Web page in the program, you can enter the corresponding URL in the EditText, so as to browse various Web pages, but also through the previous page and the next page to achieve the forward and backward function of the page, in fact, is a simple version of the browser.
Android Browsing the web in the program