Android學習之利用WebView開啟網頁

來源:互聯網
上載者:User

大家好,今天我們一起學習通過webView開啟網頁:

首先還是布局檔案main.xml的Code:

 

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /> <LinearLayout<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> ><br /><EditText<br /> android:id="@+id/editText"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1.0"<br /> android:lines="1"<br /> /><br /> <Button<br /> android:id="@+id/button"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="go"<br /> /><br /> </LinearLayout><br /> <WebView<br /> android:id="@+id/webView"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1.0"<br /> /><br /></LinearLayout>

 

利用兩個LineraLayout的嵌套顯示,上面顯示一個編輯框和按鈕,下面的是webView用來顯示網頁,大家要注意兩個LineraLayout的android:orientation的屬性,最外面的這個是垂直顯示,裡面的是水平顯示,最終得到效果

 

下面的白色地區就是webView,等Java的Code完成後就能看出效果啦,期待吧!呵呵!

 

import android.app.Activity;<br />import android.widget.*;<br />import android.os.Bundle;<br />import android.webkit.*;<br />import android.view.KeyEvent;<br />import android.view.View;<br />import android.view.View.*;<br />public class MyBrowserView extends Activity {</p><p>private EditText editText;<br />private Button button;<br />private WebView webView;</p><p> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> editText = (EditText) findViewById(R.id.editText);<br /> button = (Button) findViewById(R.id.button);<br /> webView = (WebView) findViewById(R.id.webView);</p><p> button.setOnClickListener(new OnClickListener() {<br /> //同樣為按鈕綁定點擊事件<br /> public void onClick(View v) {<br /> openBrowser();<br /> }<br /> });</p><p> editText.setOnKeyListener(new OnKeyListener() {<br /> //同樣為編輯框綁定鍵盤事件<br />@Override<br />public boolean onKey(View v, int keyCode, KeyEvent event) {<br />// TODO Auto-generated method stub<br />if(keyCode==KeyEvent.KEYCODE_ENTER) {<br />openBrowser();<br />return true;<br />}<br />return false;<br />}<br /> });<br /> }<br /> //利用webView的loadUrl方法<br /> public void openBrowser() {<br /> webView.loadUrl("http://"+editText.getText().toString());<br /> }<br />}

 

除了openBrowser()方法,其他的和用Intent開啟網頁是差不多的。

只要利用webView的loadUrl()方法就能載入網頁。好的什麼也不說啦!看:

 

 

 

 

哈哈,不錯吧,這樣就方便開啟別的網頁啦!哦!差點忘了,要在AndroidManifest.xml中加入

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

否則就不能訪問Internet喲!

到這裡看了上一篇的同學可能就問,為什麼上個例子不要加上這條語句呢?

因為通過Intent就能請求其他應用程式查看網頁,而其他應用程式需要在自己的AndroidManifest.xml中請求獲得訪問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.