標籤:webview 在程式中瀏覽網頁
本文是自己學習所做筆記,歡迎轉載,但請註明出處:http://blog.csdn.net/jesson20121020
有時需要在程式中瀏覽一些網頁,當然了可以通過調用系統的瀏覽器來開啟瀏覽,但是大多數情況下,這種方式並不適用。
下面給出如何在程式中瀏覽網頁,先看:
其實,這裡主要是利用了WebView控制項及它的一些方法。
通過WebView的loadUrl(String url)可以裝載指定的地址的網頁內容,並顯示在控制項中,上一頁和下一頁的功能分別對應於WebView的goBack()和goForward()方法;
布局檔案:main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/white" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/myEditText" android:layout_width="230dp" android:layout_height="wrap_content" android:layout_margin="10dp" /> <Button android:id="@+id/bt_go" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="轉到" android:layout_toRightOf="@id/myEditText" android:layout_margin="10dp" /> <WebView android:id="@+id/myWebView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/black" android:focusable="false" android:layout_below="@id/bt_go" android:layout_margin="10dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" > <Button android:id="@+id/bt_previous" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一頁" /> <Button android:id="@+id/bt_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一頁" /> </LinearLayout> </RelativeLayout>
主要代碼:WebViewTest
public class WebViewTest extends Activity { private Button go; private EditText mEditText1; private WebView mWebView1; private Button bt_next,bt_previous; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); go = (Button)findViewById(R.id.bt_go); bt_next = (Button)findViewById(R.id.bt_next); bt_previous = (Button)findViewById(R.id.bt_previous); go.setOnClickListener(new ClickEvent()); bt_next.setOnClickListener(new ClickEvent()); bt_previous.setOnClickListener(new ClickEvent()); mEditText1 = (EditText)findViewById(R.id.myEditText); mEditText1.setText("http://www.baidu.com"); mWebView1 = (WebView) findViewById(R.id.myWebView); mWebView1.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); //mEditText1.setText(url); Toast.makeText(WebViewTest.this, "載入完畢", Toast.LENGTH_SHORT).show(); } }); } class ClickEvent implements OnClickListener{ @Override public void onClick(View v) { switch(v.getId()){ case R.id.bt_go : /*設定抓取EditText裡面的內容*/ String strURI = (mEditText1.getText().toString()); /*?WebView裡面顯示網頁資料*/ mWebView1.loadUrl(strURI); Toast.makeText(WebViewTest.this,"正在載入"+strURI,Toast.LENGTH_LONG).show(); break; case R.id.bt_next: mWebView1.goForward(); //System.out.println(mWebView1.getUrl()); break; case R.id.bt_previous: mWebView1.goBack(); break; } } }}
代碼中onPageFinished,可以從字面來理解就是當網頁載入完畢時觸發,但是指網頁的架構載入結束,有可能圖片並未載入結束。
最後,不要忘了加訪問網路的許可權:
<uses-permission android:name="android.permission.INTERNET">
至此,就可以在程式中瀏覽網頁了,你可以在EditText輸入相應的網址,從而瀏覽各種網頁,也可以通過上一頁和下一頁實現網頁的前進和後退功能,其實就是一個簡易版的瀏覽器。
Android在程式中瀏覽網頁