Android控制項之WebView,androidwebview

來源:互聯網
上載者:User

Android控制項之WebView,androidwebview

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

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

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

<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="返回"/>        <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="重新整理"/>    </LinearLayout>    <WebView        android:id="@+id/webView"        android:layout_width="fill_parent"        android:layout_height="fill_parent"/></LinearLayout>

  最後我們開始編寫我們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/");//設定開啟的網址        mWebView.setWebChromeClient(new WebChromeClient(){            @Override            public void onReceivedTitle(WebView view, String title) {                super.onReceivedTitle(view, title);                mTextView.setText(title);//顯示開啟的網址資訊            }        });        mWebView.setWebViewClient(new WebViewClient(){            @Override            public boolean shouldOverrideUrlLoading(WebView view, String url) {                view.loadUrl(url);                return super.shouldOverrideUrlLoading(view, url);            }        });    }    //按鈕點擊事件監聽    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;            }        }    }

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

  大功告成,我們的WebView初步介紹到此結束。

 

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

聯繫我們

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