android學習十九(WebView的用法)

來源:互聯網
上載者:User

標籤:android   webview   瀏覽器   

       android提供了一個WebView控制項,藉助它我們就可以在自己的應用程式中嵌入一個瀏覽器,從而輕鬆的展示各種各樣的網頁。下面來學習下簡單的用法。建立一個WebViewTest項目,然後修改activity_main.xml中的代碼,如下所示:

<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>

     然後修改MainActivity中的代碼,如下所示:

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);//獲得WebView的執行個體webView=(WebView) findViewById(R.id.web_view);//調用getSettings()設定一些瀏覽器的屬性,調用setJavaScriptEnabled方法來讓WebView支援javascript指令碼webView.getSettings().setJavaScriptEnabled(true);/* * 調用setWebViewClient()方法,並傳入了WebViewClient的匿名類作為參數,然後重寫了 * shouldOverrideUrlLoading方法,這就表明當需要從一個網頁跳轉到另一個網頁時,我們希望目標 * 網頁仍然在當前WebView中顯示,而不是開啟系統瀏覽器。 * */webView.setWebViewClient(new WebViewClient(){@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {// TODO Auto-generated method stubview.loadUrl(url);//根據傳入的參數在去載入新的網頁return true;//表示當前WebView可以處理開啟新網頁的請求,不用藉助系統瀏覽器}});//調用loadUrl()方法,並將網址傳入,即可展示相應的網頁內容webView.loadUrl("http://www.baidu.com");}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

另外還需要注意,由於程式使用到了網路功能,而訪問網路是需要聲明許可權的,因此我們還得修改AndroidManifest.xml檔案,並加入許可權聲明,如下所示:

<?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" ><!-- 加入許可權 -->    <uses-permission android:name="android.permission.INTERNET"/>    <uses-sdk        android:minSdkVersion="13"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.jack.webviewtest.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

      

       在開始運行之前,首先需要保證你的手機或模擬器是連網的,如果使用的是模擬器,只需保證電腦能正常上網即可。然後就可以運行一下程式了,效果如下所示:



可以看到WebViewTest這個程式現在已經具備了一個簡易瀏覽器的功能,不僅成功將百度的首頁展示了出來,還可以通過點選連結瀏覽更多的網頁。

     到此,WebView的簡單使用就結束了。


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


android學習十九(WebView的用法)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.