Android网页浏览器的开发

Android网页浏览器的核心Widget是包含了WebKit的WebView。

首先,布局文件activity_main.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center" >
 
        <Button
            android:id="@+id/btnEar"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="<<" />
 
        <Button
            android:id="@+id/btnPre"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="<" />
 
        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text=">" />
 
        <Button
            android:id="@+id/btnLast"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text=">>" />
 
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="40dp" >
 
        <EditText
            android:id="@+id/edtUrl"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="5"
            android:ems="10"
            android:singleLine="true" >
 
            <requestFocus />
        </EditText>
 
        <Button
            android:id="@+id/btnGo"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="GO" />
 
    </LinearLayout>
 
    <WebView
        android:id="@+id/webMain"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
 
</LinearLayout>

布局文件相对简单,包含了几个Button,一个EditText,一个WebView。

Java文件MainActivity.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.hzhi.mybrowser;
 
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity implements OnClickListener{
     
    // 控件
    Button btnEar;
    Button btnPre;
    Button btnNext;
    Button btnLast;
    EditText edtUrl;
    Button btnGo;
    WebView webMain;
     
    // URL
    String strUrl;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获得控件
        getCon();
        webMain.setWebViewClient(new MyWebViewClient());
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
     
    // 获得控件
    public void getCon(){
        btnEar = (Button) findViewById(R.id.btnEar);
        btnPre = (Button) findViewById(R.id.btnPre);
        btnNext = (Button) findViewById(R.id.btnNext);
        btnLast = (Button) findViewById(R.id.btnLast);
        edtUrl = (EditText) findViewById(R.id.edtUrl);
        btnGo = (Button) findViewById(R.id.btnGo);
        webMain = (WebView) findViewById(R.id.webMain);
         
        btnEar.setOnClickListener(this);
        btnPre.setOnClickListener(this);
        btnNext.setOnClickListener(this);
        btnLast.setOnClickListener(this);
        edtUrl.setOnClickListener(this);
        btnGo.setOnClickListener(this);
         
        // 设置JavaScript可用
        webMain.getSettings().setJavaScriptEnabled(true);
        webMain.setScrollBarStyle(View.SCROLLBAR_POSITION_DEFAULT);
    }
 
    @Override
    public void onClick(View v) {
        if (v==btnEar)
        {
            if (webMain.canGoBackOrForward(-2)){
                webMain.goBackOrForward(-2);
            }
        }
        else if (v==btnPre)
        {
            if (webMain.canGoBack()){
                webMain.goBack();
            }          
        }
        else if (v==btnNext)
        {
            if (webMain.canGoForward()){
                webMain.goForward();
            }
        }
        else if (v==btnLast)
        {
            if (webMain.canGoBackOrForward(2)){
                webMain.goBackOrForward(2);
            }
        }
        else if (v==edtUrl)
        {
             
        }
        // 下载网页
        else if (v==btnGo)
        {
            strUrl = edtUrl.getText().toString();
            webMain.loadUrl("http://" + strUrl);
        }
    }
     
    // WebViewClient的处理类
    class MyWebViewClient extends WebViewClient {
        @Override
        public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
            btnPre.setEnabled(webMain.canGoBack());
            btnNext.setEnabled(webMain.canGoForward());
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            if (webMain.getTitle() != null) {
                MainActivity.this.setTitle(webMain.getTitle());
            }
        }
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            MainActivity.this.setTitle(url);
            edtUrl.setText(url);
            btnPre.setEnabled(webMain.canGoBack());
            btnNext.setEnabled(webMain.canGoForward());
        }
    }
 
}

Java文件主要是对WebView控件的操作。其中MyWebViewClient继承自WebViewClient,有更新网页访问历史(onUpdateVisitedHistory)、网页结束(onPageFinished)、网页开始(onPageStarted)等几个函数。

WebView.setWebViewClient(new MyWebViewClient())表示new一个MyWebViewClient实例作为浏览器,如果没有这行代码,会以Android系统已安装的浏览器浏览网页。

单击GO按钮时,使用loadUrl函数访问网页。

最后,在Manifest文件里面加*问网络的android.permission.INTERNET权限,否则无法打开网页。

运行效果。

Android网页浏览器的开发

Android网页浏览器的开发

上一篇:《深入理解Android 卷1》读书笔记 (一)—— Android Init之属性服务 (property_service)


下一篇:什么是AutoMapper?