android-WebView類用法初探

來源:互聯網
上載者:User

要把項目改成android版本的,用到WebView類:

1、首先必須在AndroidManifest.xml裡配置如下:

  目的是允許該控制項訪問瀏覽器資源

  </activity>            </application>    <uses-permission android:name="android.permission.INTERNET" />

2、在你所要載入的設定檔裡面配置個WebView(layout檔案夾,main.xml):

<TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <WebView         android:id="@+id/WebView01"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/webview"        />

3、邏輯代碼如下:

     private WebView webview;
     private String url="file:///android_asset/show.html";
               //訪問本地檔案show.html,檔案須放在工程檔案的asset目錄
  //url="content://com.android.htmlfileprovider/sdcard/index.html";
-----開啟本地sd卡內的index.html檔案
//url=("http://wap.baidu.com);
-----開啟指定URL的html檔案
     .
     .
     .    
     webview=(WebView)findViewById(R.id.WebView01); webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl(url);

4、訪問web或者資源的其他方法:

直接用Url訪問:

 Uri uri = Uri.parse("http://www.example.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);

2.使用TextView顯示HTML方法

TextView text1 = (TextView)findViewById(R.id.TextView02);text1.setText(Html.fromHtml(“<font size='20'>網頁內容</font>”));
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.