Android Development: The WebView of controls

Source: Internet
Author: User
Tags xmlns

How do I open a Web site in an android application? Google has provided us with a solution, now let's take a look at the WebView control.

In order to facilitate the summary, to achieve the following effect as the main line, to summarize:

First we look at its layout file, the entire interface is divided into two parts, the top is a similar to the title bar effect, it is composed of two button buttons and a TextView, the lower part is a WebView control, Remove the system title through Androidmanifest.xml (if you don't understand, check my previous blog: Android Common Properties), has reached the above image effect. To facilitate self-study, the following code:

  1. <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
  2. Xmlns:tools= "Http://schemas.android.com/tools"
  3. Android:layout_width= "Match_parent"
  4. android:layout_height= "Match_parent"
  5. android:orientation= "Vertical"
  6. Tools:context= ". Mainactivity ">
  7. <linearlayout
  8. Android:layout_width= "Fill_parent"
  9. android:layout_height= "Wrap_content"
  10. android:weightsum= "1" >
  11. <button
  12. Android:id= "@+id/quit"
  13. Android:layout_gravity= "left"
  14. Android:layout_width= "Wrap_content"
  15. android:layout_height= "Wrap_content"
  16. android:text= "Back"/>
  17. <textview
  18. Android:id= "@+id/web"
  19. android:layout_gravity= "Center"
  20. android:gravity= "Center"
  21. Android:layout_width= "222DP"
  22. android:layout_height= "Wrap_content"
  23. android:layout_weight= "1.13"/>
  24. <button
  25. Android:id= "@+id/news"
  26. Android:layout_gravity= "Right"
  27. Android:layout_width= "Wrap_content"
  28. android:layout_height= "Wrap_content"
  29. android:text= "Refresh"/>
  30. </LinearLayout>
  31. <webview
  32. Android:id= "@+id/webview"
  33. Android:layout_width= "Fill_parent"
  34. android:layout_height= "Fill_parent"/>
  35. </LinearLayout>

Finally we start writing our Mainactivity.java:

  1. public class Mainactivity extends activity {
  2. Private TextView Mtextview;
  3. Private WebView Mwebview;
  4. Private Button Mbreak;
  5. Private Button mnews;
  6. @Override
  7. protected void OnCreate (Bundle savedinstancestate) {
  8. Super.oncreate (savedinstancestate);
  9. Setcontentview (R.layout.activity_main);
  10. Init ();
  11. }
  12. public void init () {
  13. Mtextview = (TextView) Findviewbyid (R.id.web);
  14. Mwebview = (webview) Findviewbyid (R.id.webview);
  15. Mbreak = (Button) Findviewbyid (r.id.quit);
  16. Mnews = (Button) Findviewbyid (r.id.news);
  17. Mbreak.setonclicklistener (New MyListener ());
  18. Mnews.setonclicklistener (New MyListener ());
  19. Mwebview.loadurl ("http://www.baidu.com/");/Set Open URLs
  20. Mwebview.setwebchromeclient (New Webchromeclient () {
  21. @Override
  22. public void Onreceivedtitle (WebView view, String title) {
  23. Super.onreceivedtitle (view, title);
  24. Mtextview.settext (title);//Display open URL information
  25. }
  26. });
  27. Mwebview.setwebviewclient (New Webviewclient () {
  28. @Override
  29. public boolean shouldoverrideurlloading (webview view, String URL) {
  30. View.loadurl (URL);
  31. Return super.shouldoverrideurlloading (view, URL);
  32. }
  33. });
  34. }
  35. button click event Monitor
  36. Class MyListener implements view.onclicklistener{
  37. @Override
  38. public void OnClick (view view) {
  39. Switch (View.getid ()) {
  40. Case R.id.quit:
  41. Finish ();
  42. Break
  43. Case R.id.news:
  44. Mwebview.reload ();
  45. Break
  46. }
  47. }
  48. }

Finally, don't forget to add the use of the network statement in Androidmanifest.xml: <uses-permission android:name= "Android.permission.INTERNET"/>

Finished, our WebView preliminary introduction to this end.

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.