This article is my 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. Of course, you can turn on browsing by invoking the browser of the system. But in most cases, such a method does not apply.
Here's how to browse the Web in a program. Look first:
In fact, the main thing here is to take advantage of the WebView control and some of its methods.
The WebView loadurl (String URL) is able to load the Web page content of the specified address, and is displayed 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 "Android: Layout_alignparentbottom= "True" android:layout_centerhorizontal= "true" > <button android:i D= "@+id/bt_previous" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:text= "Previous"/> <button android:id= "@+id/bt_next" android:layout_width= "Wrap_c Ontent "android:layout_height=" Wrap_content "android:text=" Next "/> </linearlayout& Gt </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 onpagefinished in the code can be literally understood to be triggered when the page loading is complete, but when the frame of the Web page is loaded, it is possible that the picture is not loaded at the end.
Finally, don't forget to add access to the network:
At this point, you will be able to browse the Web in the program. You can enter the corresponding URL in the EditText, so that you can browse various Web pages, but also through the previous page and the next page to achieve the forward and backward features of the page. is actually a simple version of the browser.
Android Browsing the web in the program