Android在程序中瀏覽網(wǎng)頁
來源:程序員人生 發(fā)布時(shí)間:2014-11-07 08:34:42 閱讀次數(shù):3366次
本文是自己學(xué)習(xí)所做筆記,歡迎轉(zhuǎn)載,但請(qǐng)注明出處:http://blog.csdn.net/jesson20121020
有時(shí)需要在程序中閱讀1些網(wǎng)頁,固然了可以通過調(diào)用系統(tǒng)的閱讀器來打開閱讀,但是大多數(shù)情況下,這類方式其實(shí)不適用。
下面給出如何在程序中閱讀網(wǎng)頁,先看效果圖:


其實(shí),這里主要是利用了WebView控件及它的1些方法。
通過WebView的loadUrl(String url)可以裝載指定的地址的網(wǎng)頁內(nèi)容,并顯示在控件中,上1頁和下1頁的功能分別對(duì)應(yīng)于WebView的goBack()和goForward()方法;
布局文件:main.xml
<?xml version="1.0" encoding="utf⑻"?>
<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="轉(zhuǎn)到"
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="上1頁"
/>
<Button
android:id="@+id/bt_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下1頁"
/>
</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 :
/*設(shè)定抓取EditText里面的內(nèi)容*/
String strURI = (mEditText1.getText().toString());
/*?WebView里面顯示網(wǎng)頁數(shù)據(jù)*/
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,可以從字面來理解就是當(dāng)網(wǎng)頁加載終了時(shí)觸發(fā),但是指網(wǎng)頁的框架加載結(jié)束,有可能圖片并未加載結(jié)束。
最后,不要忘了加訪問網(wǎng)絡(luò)的權(quán)限:
<uses-permission android:name="android.permission.INTERNET">
至此,就能夠在程序中閱讀網(wǎng)頁了,你可以在EditText輸入相應(yīng)的網(wǎng)址,從而閱讀各種網(wǎng)頁,也能夠通過上1頁和下1頁實(shí)現(xiàn)網(wǎng)頁的前進(jìn)和后退功能,其實(shí)就是1個(gè)簡(jiǎn)易版的閱讀器。
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)