自己写的安卓简易浏览器

来源:互联网 发布:mac os 双系统 编辑:程序博客网 时间:2024/04/29 21:50

自己写的安卓简易浏览器


程序:http://pan.baidu.com/share/link?shareid=2944813691&uk=2651469454

源代码:http://pan.baidu.com/share/link?shareid=2926499688&uk=2651469454


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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.example.webbrowsertest;
 
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
    private Button go_button, exit_button, top_button, bottom_button, baidu_button, pre_button, next_button;
    private EditText url_EditText;
    private TextView des_TextView;
    private WebView browser_webView;
    private float x, y;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        go_button = (Button) findViewById(R.id.go_button);
        exit_button = (Button) findViewById(R.id.exit_button);
        top_button = (Button) findViewById(R.id.top_button);
        bottom_button = (Button) findViewById(R.id.bottom_button);
        baidu_button = (Button) findViewById(R.id.baidu_button);
        pre_button = (Button) findViewById(R.id.pre_button);
        next_button = (Button) findViewById(R.id.next_button);
        url_EditText = (EditText) findViewById(R.id.url_EditText);
        des_TextView=(TextView)findViewById(R.id.des_TextView);
        browser_webView = (WebView) findViewById(R.id.browser_webView);
 
        url_EditText.setSingleLine();
 
        WebSettings webSettings = browser_webView.getSettings();
 
        webSettings.setJavaScriptEnabled(true);//设置支持javascript
 
        webSettings.setAllowFileAccess(true);//设置支持文件访问
 
        webSettings.setBuiltInZoomControls(true);//设置支持缩放
 
        webSettings.setDisplayZoomControls(false);//设置隐藏缩放按钮
 
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);//设置优先级
 
        webSettings.setUseWideViewPort(true);//设置缩放自适应屏幕1
 
        webSettings.setLoadWithOverviewMode(true);//设置缩放自适应屏幕2
 
        browser_webView.setWebChromeClient(new MyWebChromeClient());
 
        go_button.setOnClickListener(new go_buttonOnClickListener());
 
        exit_button.setOnClickListener(new exit_buttonOnClickListener());
 
        top_button.setOnClickListener(new top_buttonOnClickListener());
 
        bottom_button.setOnClickListener(new bottom_buttonOnClickListener());
 
        baidu_button.setOnClickListener(new baidu_buttonOnClickListener());
 
        pre_button.setOnClickListener(new pre_buttonOnClickListener());
 
        next_button.setOnClickListener(new next_buttonOnClickListener());
 
        browser_webView.setOnTouchListener(new browser_webViewOnTouchListener());
 
        browser_webView.loadUrl("http://www.baidu.com");
 
        browser_webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                url_EditText.setText(url);
                return true;
            }
        });
 
    }
 
    class go_buttonOnClickListener implements View.OnClickListener {
 
        @Override
        public void onClick(View view) {
            String url = url_EditText.getText().toString();
            if (url.contains("http://")) {
                browser_webView.loadUrl(url);
            }else if(url.contains("https://")){
                browser_webView.loadUrl(url);
            }else if(url.contains("")){
                browser_webView.loadUrl(url);
            }else if(url.contains("")){
                browser_webView.loadUrl(url);
            else {
                browser_webView.loadUrl("http://" + url);
            }
            exit_button.setVisibility(View.INVISIBLE);
        }
    }
 
    class exit_buttonOnClickListener implements View.OnClickListener {
 
        @Override
        public void onClick(View view) {
            finish();
        }
    }
 
    class top_buttonOnClickListener implements View.OnClickListener {
 
        @Override
        public void onClick(View view) {
            browser_webView.setScrollY(0);
        }
    }
 
 
    class bottom_buttonOnClickListener implements View.OnClickListener {
 
        @Override
        public void onClick(View view) {
            browser_webView.setScrollY((int) (browser_webView.getContentHeight() * browser_webView.getScale() - (browser_webView.getHeight() + browser_webView.getScrollY())));
        }
    }
 
    class browser_webViewOnTouchListener implements View.OnTouchListener {
 
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (exit_button.getVisibility() == View.VISIBLE) {
                    exit_button.setVisibility(View.INVISIBLE);
                }
 
                des_TextView.setVisibility(View.INVISIBLE);
 
                if (
                        baidu_button.getVisibility() == View.VISIBLE &&
                                pre_button.getVisibility() == View.VISIBLE &&
                                next_button.getVisibility() == View.VISIBLE) {
                    baidu_button.setVisibility(View.INVISIBLE);
                    pre_button.setVisibility(View.INVISIBLE);
                    next_button.setVisibility(View.INVISIBLE);
                }
                //Toast.makeText(MainActivity.this,browser_webView.getX()+" "+browser_webView.getY()+" "+browser_webView.getWidth()+" "+browser_webView.getHeight(),Toast.LENGTH_LONG).show();
            }
 
            //3指退出程序
            if (event.getPointerCount() == 3 && event.getAction() == MotionEvent.ACTION_MOVE) {
                finish();
            }
 
            //4指显示菜单
            if (event.getPointerCount() == 4) {
                baidu_button.setVisibility(View.VISIBLE);
                pre_button.setVisibility(View.VISIBLE);
                next_button.setVisibility(View.VISIBLE);
            }
 
            return false;
        }
    }
 
    class baidu_buttonOnClickListener implements View.OnClickListener {
 
        @Override
        public void onClick(View view) {
            browser_webView.loadUrl("http://www.baidu.com");
            url_EditText.setText(browser_webView.getUrl());
            baidu_button.setVisibility(View.INVISIBLE);
            pre_button.setVisibility(View.INVISIBLE);
            next_button.setVisibility(View.INVISIBLE);
        }
    }
 
    class pre_buttonOnClickListener implements View.OnClickListener {
 
        @Override
        public void onClick(View view) {
            browser_webView.goBack();
            baidu_button.setVisibility(View.INVISIBLE);
            pre_button.setVisibility(View.INVISIBLE);
            next_button.setVisibility(View.INVISIBLE);
        }
    }
 
    class next_buttonOnClickListener implements View.OnClickListener {
 
        @Override
        public void onClick(View view) {
            browser_webView.goForward();
            baidu_button.setVisibility(View.INVISIBLE);
            pre_button.setVisibility(View.INVISIBLE);
            next_button.setVisibility(View.INVISIBLE);
        }
    }
 
    class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
 
            new AlertDialog.Builder(MainActivity.this).setTitle("AlertDialog").setMessage(message)
 
                    .setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
 
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            result.confirm();
                        }
                    }).setCancelable(false).show();
            return true;
        }
 
        @Override
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            new AlertDialog.Builder(MainActivity.this).setTitle("ConfirmDialog").setMessage(message).setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
 
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    result.confirm();
                }
            })
 
                    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
 
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            result.confirm();
                        }
                    }).setCancelable(false).show();
            return true;
        }
 
        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
 
            final LayoutInflater factory = LayoutInflater.from(MainActivity.this);
 
 
            final View dialogView = factory.inflate(R.layout.prompt_view, null);
 
 
            ((TextView) dialogView.findViewById(R.id.text)).setText(message);
 
 
            ((EditText) dialogView.findViewById(R.id.edit)).setText(defaultValue);
 
            new AlertDialog.Builder(MainActivity.this).setTitle("PromptDialog")
 
                    .setView(dialogView).setPositiveButton(android.R.string.ok,new AlertDialog.OnClickListener() {
 
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
 
                    String value = ((EditText) dialogView.findViewById(R.id.edit)).getText().toString();
                    result.confirm();
                }
            }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
 
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    result.confirm();
                }
            }).show();
            return true;
        }
    }
 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && browser_webView.canGoBack()) {
            browser_webView.goBack();
            exit_button.setVisibility(View.VISIBLE);//按返回键时显示退出按钮
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}



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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<RelativeLayout 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"
    tools:context=".MainActivity"
    android:paddingTop="5dp">
 
    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="90px"
            android:id="@+id/RelativeLayout">
 
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="80px"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/input_label"
                android:id="@+id/input_label"
                android:textAlignment="center"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:paddingTop="12dp"
                android:paddingLeft="10px"/>
 
        <EditText
                android:layout_width="wrap_content"
                android:layout_height="80px"
                android:id="@+id/url_EditText"
                android:layout_toLeftOf="@+id/go_button"
                android:hint="@string/piurl"
                android:singleLine="true"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/input_label"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"/>
 
        <Button
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="80px"
                android:text="@string/go_button"
                android:id="@+id/go_button"
                android:layout_alignParentRight="true"
                android:layout_alignWithParentIfMissing="false"
                android:width="20px"
                android:gravity="center"
                android:layout_alignParentTop="true"
                android:layout_marginRight="5dp"/>
    </RelativeLayout>
 
    <WebView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/browser_webView"
            android:layout_marginLeft="0dp"
            android:layout_marginBottom="0dp"
            android:layout_margin="0dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:padding="0dp"
            android:paddingLeft="0dp"
            android:paddingTop="0dp"
            android:paddingRight="0dp"
            android:paddingBottom="0dp"
            android:layout_below="@+id/RelativeLayout"
            android:layout_alignParentRight="true"/>
 
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/exit_button"
            android:id="@+id/exit_button"
            android:layout_alignBottom="@+id/browser_webView"
            android:layout_alignRight="@+id/browser_webView"
            android:alpha="0.4"
            android:visibility="invisible"/>
 
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/bottom_button"
            android:id="@+id/bottom_button"
            android:alpha="0.4"
            android:layout_above="@+id/top_button"
            android:layout_alignLeft="@+id/top_button"/>
 
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/top_button"
            android:id="@+id/top_button"
            android:layout_alignBaseline="@+id/browser_webView"
            android:layout_alignBottom="@+id/browser_webView"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:alpha="0.4"/>
 
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/baidu_button"
            android:id="@+id/baidu_button"
            android:layout_below="@+id/RelativeLayout"
            android:visibility="invisible"
            android:layout_marginLeft="20dp"
            android:alpha="0.5"/>
 
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/pre_button"
            android:id="@+id/pre_button"
            android:layout_below="@+id/baidu_button"
            android:visibility="invisible"
            android:layout_marginLeft="20dp"
            android:alpha="0.5"/>
 
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next_button"
            android:id="@+id/next_button"
            android:layout_below="@+id/pre_button"
            android:visibility="invisible"
            android:layout_marginLeft="20dp"
            android:alpha="0.5"/>
 
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"
            android:ems="10"
            android:id="@+id/des_TextView"
            android:layout_centerInParent="true"
            android:width="220dp"
            android:text="@string/des"
            android:textAlignment="center"
            android:textSize="@dimen/des"
            android:textColor="#030201"
            android:background="#5cbfff"
            android:padding="20dp"
            android:alpha="0.8"
            android:editable="false"/>
 
</RelativeLayout>
原创粉丝点击