Android Learning 19 (usage of WebView)

Source: Internet
Author: User

Android provides a WebView control that allows us to embed a browser in our own application to easily display a wide variety of web pages. Let's learn the simple usage below. Create a new Webviewtest project, and then modify the code in the Activity_main.xml as follows:

<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 "         >    <webview         android:id= "@+id/web_view"        android:layout_width= "Match_parent"        android: layout_height= "Match_parent"        /></linearlayout>

Then modify the code in the mainactivity as follows:

Package Com.jack.webviewtest;import Android.os.bundle;import Android.app.activity;import android.view.Menu;import Android.webkit.webview;import Android.webkit.webviewclient;public class Mainactivity extends Activity {private WebView WebView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//Get WebView instance webview= (WebView) Findviewbyid (R.id.web_view);// Call GetSettings () to set some browser properties, call the Setjavascriptenabled method to let WebView support JavaScript script webview.getsettings (). Setjavascriptenabled (TRUE);/* Call the Setwebviewclient () method and pass in the anonymous class of webviewclient as the parameter, and then rewrite the * Shouldoverrideurlloading method, which means that when you need to jump from one page to another, we want the destination * page to still appear in the current webview instead of opening the system browser. * */webview.setwebviewclient (new webviewclient () {@Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {//TODO auto-generated method Stubview.loadurl (URL);//To load a new Web page return based on the parameters passed in true;// Indicates that the current webview can handle the request to open a new webpage without the help of the System browser});//Call the Loadurl () method and pass in the URL to display the corresponding page content Webview.loaDurl ("http://www.baidu.com");} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

It is also important to note that because the program uses network functionality and access to the network requires declarative permissions, we also have to modify the Androidmanifest.xml file and add the permission declaration as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/     Android "package=" Com.jack.webviewtest "android:versioncode=" 1 "android:versionname=" 1.0 "><!--join Privileges--        <uses-permission android:name= "Android.permission.INTERNET"/> <uses-sdk android:minsdkversion= "13" android:targetsdkversion= "/> <application android:allowbackup=" true "android:icon=" @draw            Able/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity Android:name= "com.jack.webviewtest.MainActivity" android:label= "@string/app_name" > &L t;intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category Android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </a Pplication></manifest>

Before starting to run, you need to ensure that your phone or simulator is networked, if you are using an emulator, just make sure the computer can surf the internet properly. Then you can run the program, the effect is as follows:



Can see webviewtest This program now has a simple browser features, not only the success of the Baidu Home Show out, you can also click the link to browse more pages.

By this, the simple use of webview is over.


http://blog.csdn.net/j903829182/article/details/42438827


Android Learning 19 (usage of WebView)

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.