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:
- <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
- Xmlns:tools= "Http://schemas.android.com/tools"
- Android:layout_width= "Match_parent"
- android:layout_height= "Match_parent"
- android:orientation= "Vertical"
- Tools:context= ". Mainactivity ">
- <linearlayout
- Android:layout_width= "Fill_parent"
- android:layout_height= "Wrap_content"
- android:weightsum= "1" >
- <button
- Android:id= "@+id/quit"
- Android:layout_gravity= "left"
- Android:layout_width= "Wrap_content"
- android:layout_height= "Wrap_content"
- android:text= "Back"/>
- <textview
- Android:id= "@+id/web"
- android:layout_gravity= "Center"
- android:gravity= "Center"
- Android:layout_width= "222DP"
- android:layout_height= "Wrap_content"
- android:layout_weight= "1.13"/>
- <button
- Android:id= "@+id/news"
- Android:layout_gravity= "Right"
- Android:layout_width= "Wrap_content"
- android:layout_height= "Wrap_content"
- android:text= "Refresh"/>
- </LinearLayout>
- <webview
- Android:id= "@+id/webview"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Fill_parent"/>
- </LinearLayout>
Finally we start writing our Mainactivity.java:
- public class Mainactivity extends activity {
- Private TextView Mtextview;
- Private WebView Mwebview;
- Private Button Mbreak;
- Private Button mnews;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Init ();
- }
- public void init () {
- Mtextview = (TextView) Findviewbyid (R.id.web);
- Mwebview = (webview) Findviewbyid (R.id.webview);
- Mbreak = (Button) Findviewbyid (r.id.quit);
- Mnews = (Button) Findviewbyid (r.id.news);
- Mbreak.setonclicklistener (New MyListener ());
- Mnews.setonclicklistener (New MyListener ());
- Mwebview.loadurl ("http://www.baidu.com/");/Set Open URLs
- Mwebview.setwebchromeclient (New Webchromeclient () {
- @Override
- public void Onreceivedtitle (WebView view, String title) {
- Super.onreceivedtitle (view, title);
- Mtextview.settext (title);//Display open URL information
- }
- });
- Mwebview.setwebviewclient (New Webviewclient () {
- @Override
- public boolean shouldoverrideurlloading (webview view, String URL) {
- View.loadurl (URL);
- Return super.shouldoverrideurlloading (view, URL);
- }
- });
- }
- button click event Monitor
- Class MyListener implements view.onclicklistener{
- @Override
- public void OnClick (view view) {
- Switch (View.getid ()) {
- Case R.id.quit:
- Finish ();
- Break
- Case R.id.news:
- Mwebview.reload ();
- Break
- }
- }
- }
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.