Android開發:控制項之WebView

來源:互聯網
上載者:User

Android開發:控制項之WebView

 如何在Android應用中開啟Web網站呢?Google為我們提供瞭解決方案,現在就讓我們一起看一下WebView控制項吧。

為了方便總結,就以實現下面這個效果為主線,進行總結:

首先我們先看一下它的布局檔案吧,整個介面分為上下兩個部分,上部是一個類似於標題列的效果,它是由兩個Button按鈕和一個TextView組成的,下部是一個WebView控制項,通過AndroidManifest.xml去除系統的標題(如有不懂,請查閱我的上一遍部落格:Android常用屬性),已達到效果。為方便大家自學,下面奉上代碼:

 
  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.  
  8.     <LinearLayout 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="wrap_content" 
  11.         android:weightSum="1"> 
  12.         <Button 
  13.             android:id="@+id/quit" 
  14.             android:layout_gravity="left" 
  15.             android:layout_width="wrap_content" 
  16.             android:layout_height="wrap_content" 
  17.             android:text="返回"/> 
  18.         <TextView 
  19.             android:id="@+id/web" 
  20.             android:layout_gravity="center" 
  21.             android:gravity="center" 
  22.             android:layout_width="222dp" 
  23.             android:layout_height="wrap_content" 
  24.             android:layout_weight="1.13" /> 
  25.         <Button 
  26.             android:id="@+id/news" 
  27.             android:layout_gravity="right" 
  28.             android:layout_width="wrap_content" 
  29.             android:layout_height="wrap_content" 
  30.             android:text="重新整理"/> 
  31.     </LinearLayout> 
  32.     <WebView 
  33.         android:id="@+id/webView" 
  34.         android:layout_width="fill_parent" 
  35.         android:layout_height="fill_parent"/> 
  36.  
  37. </LinearLayout> 

最後我們開始編寫我們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/");//設定開啟的網址 
  20.  
  21.         mWebView.setWebChromeClient(new WebChromeClient(){ 
  22.             @Override 
  23.             public void onReceivedTitle(WebView view, String title) { 
  24.                 super.onReceivedTitle(view, title); 
  25.                 mTextView.setText(title);//顯示開啟的網址資訊 
  26.             } 
  27.         }); 
  28.  
  29.         mWebView.setWebViewClient(new WebViewClient(){ 
  30.             @Override 
  31.             public boolean shouldOverrideUrlLoading(WebView view, String url) { 
  32.                 view.loadUrl(url); 
  33.                 return super.shouldOverrideUrlLoading(view, url); 
  34.             } 
  35.         }); 
  36.     } 
  37.  
  38.     //按鈕點擊事件監聽 
  39.     class myListener implements View.OnClickListener{ 
  40.         @Override 
  41.         public void onClick(View view) { 
  42.             switch (view.getId()){ 
  43.                 case R.id.quit : 
  44.                     finish(); 
  45.                     break; 
  46.                 case R.id.news : 
  47.                     mWebView.reload(); 
  48.                     break; 
  49.             } 
  50.         } 
  51.     } 

最後不要忘在AndroidManifest.xml中添加使用網路聲明:<uses-permission android:name="android.permission.INTERNET"/>

大功告成,我們的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.