标签:
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
|
package
com.rong05.webview; import
java.io.FileInputStream; import
java.io.FileNotFoundException; import
java.io.FileOutputStream; import
java.io.IOException; import
java.util.ArrayList; import
java.util.HashMap; import
java.util.List; import
java.util.Map; import
android.app.Activity; import
android.os.Bundle; import
android.view.View; import
android.view.View.OnClickListener; import
android.widget.AdapterView; import
android.widget.AdapterView.OnItemClickListener; import
android.widget.Button; import
android.widget.ListView; import
android.widget.SimpleAdapter; public
class
History extends
Activity { ListView
list; Button
back,clear; private
List<Map<String,String>> historyList = new
ArrayList<Map<String,String>>(); SimpleAdapter
adapter; String
wordsTitle[]; String
wordsHistory[]; @Override protected
void
onCreate(Bundle savedInstanceState) { //
TODO Auto-generated method stub super .onCreate(savedInstanceState); super .setContentView(R.layout.history); list=(ListView)
findViewById(R.id.historylist); back=(Button)
findViewById(R.id.back); clear=(Button)
findViewById(R.id.clear); getHistory(); getTitles(); for ( int
i= 0 ;i<wordsHistory.length&&i<wordsTitle.length;i++){ Map<String,String>
map= new
HashMap<String, String>(); map.put( "title" ,wordsTitle[i]); map.put( "history" ,wordsHistory[i]); historyList.add(map); } adapter= new
SimpleAdapter(getApplicationContext(),historyList,R.layout.data_list, new
String[]{ "title" , "history" }, new
int []{R.id.textView1,R.id.textView2}); list.setAdapter(adapter); list.setOnItemClickListener(listlistener); back.setOnClickListener(backListener); clear.setOnClickListener(clearListener); } //读取history.txt中的历史信息 void
getHistory(){ FileInputStream
fis= null ; try
{ fis=openFileInput( "history.txt" );
}
catch
(FileNotFoundException e) { //
TODO Auto-generated catch block e.printStackTrace(); } try
{ byte []
bate= new
byte [fis.available()]; while (fis.read(bate)!=- 1 ){ } String
str= new
String(bate); wordsHistory=str.split( ";" ); }
catch
(IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); } } //读取title.txt中的网页标题 void
getTitles(){ FileInputStream
fis= null ; try
{ fis=openFileInput( "title.txt" );
}
catch
(FileNotFoundException e) { //
TODO Auto-generated catch block e.printStackTrace(); } try
{ byte []
bate= new
byte [fis.available()]; while (fis.read(bate)!=- 1 ){ } String
str= new
String(bate); wordsTitle=str.split( ";" ); }
catch
(IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); } } //实现ListView的事件 OnItemClickListener
listlistener= new
OnItemClickListener() { @Override public
void
onItemClick(AdapterView<?> arg0, View arg1, int
arg2, long
arg3) { //
TODO Auto-generated method stub //将信息返回到mainActivity中 History. this .getIntent().putExtra( "retmsg" ,historyList.get(arg2).get( "history" )); History. this .setResult(RESULT_OK,History. this .getIntent()); History. this .finish(); } }; //对返回按键的监听 OnClickListener
backListener= new
OnClickListener() { @Override public
void
onClick(View v) { //
TODO Auto-generated method stub History. this .setResult(RESULT_CANCELED,
getIntent()); //停止当前页面 History. this .finish(); } }; //对清除历史记录按键的监听 OnClickListener
clearListener= new
OnClickListener() { @Override public
void
onClick(View v) { //
TODO Auto-generated method stub deleteHistory(); deleteTitle(); //让ListView清空 list.removeAllViewsInLayout(); } }; //删除history.txt的内容 void
deleteHistory(){ FileOutputStream
fos= null ; try
{ fos=openFileOutput( "history.txt" ,MODE_PRIVATE); try
{ fos.write( "" .getBytes()); }
catch
(IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); } }
catch
(FileNotFoundException e) { //
TODO Auto-generated catch block e.printStackTrace(); }
try
{ fos.flush(); fos.close(); }
catch
(IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); } } //删除title.txt的内容 void
deleteTitle(){ FileOutputStream
fos= null ; try
{ fos=openFileOutput( "title.txt" ,MODE_PRIVATE); try
{ fos.write( "" .getBytes()); }
catch
(IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); } }
catch
(FileNotFoundException e) { //
TODO Auto-generated catch block e.printStackTrace(); }
try
{ fos.flush(); fos.close(); }
catch
(IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); } } } |
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
295
296
297
298
299
300
|
package
com.rong05.webview; import
java.io.FileNotFoundException; import
java.io.FileOutputStream; import
java.io.IOException; import
android.annotation.SuppressLint; import
android.app.Activity; import
android.content.Intent; import
android.graphics.Bitmap; import
android.os.Bundle; import
android.util.FloatMath; import
android.view.KeyEvent; import
android.view.Menu; import
android.view.MotionEvent; import
android.view.View; import
android.view.View.OnClickListener; import
android.webkit.ValueCallback; 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.FrameLayout; import
android.widget.ProgressBar; public
class
WebMainActivity extends
Activity { Button
go,next,last,history; EditText
input; WebView
web; ProgressBar
progress; static
double
beforeLenght= 0 ,changeLenght= 0 ; static
String FILE_NAME= "history.txt" ; @SuppressLint ( "SetJavaScriptEnabled" )
@Override protected
void
onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_web_main); findView(); web.loadUrl( "http://baidu.com" ); web.setWebViewClient(client); WebSettings
setter=web.getSettings(); //实现JavaScrip setter.setJavaScriptEnabled( true ); //实现网页的缩放 setter.setBuiltInZoomControls( true ); setter.setJavaScriptCanOpenWindowsAutomatically( true ); setter.setGeolocationEnabled( true ); web.setWebChromeClient(chromeClient);
go.setOnClickListener( new
Action()); next.setOnClickListener( new
Action()); last.setOnClickListener( new
Action()); history.setOnClickListener( new
Action()); if (WebMainActivity.changeLenght>WebMainActivity.beforeLenght) web.zoomIn();
else if (WebMainActivity.changeLenght<WebMainActivity.beforeLenght) web.zoomOut(); } /** *
对WebView的实现其中的方法 *
*/ WebViewClient
client= new
WebViewClient(){ //该方法是让网页在WebView中显示不调用本地浏览器 @Override public
boolean
shouldOverrideUrlLoading(WebView view, String url) { //
TODO Auto-generated method stub return
super .shouldOverrideUrlLoading(view,
url); } //对历史记录的刷新 @Override public
void
doUpdateVisitedHistory(WebView view, String url, boolean
isReload) { //
TODO Auto-generated method stub last.setEnabled(web.canGoBack());
next.setEnabled(web.canGoForward()); super .doUpdateVisitedHistory(view,
url, isReload); } //在读完网页后对其进行的操作 @Override public
void
onPageFinished(WebView view, String url) { //设置程序的标题为网页的标题
if
(web.getTitle() != null )
{ WebMainActivity. this .setTitle(web.getTitle());
getTitles(web.getTitle()); getHistory(url); }
} //在开始读取网页时对其进行的操作 @Override public
void
onPageStarted(WebView view, String url, Bitmap favicon) { //
TODO Auto-generated method stub WebMainActivity. this .setTitle( "Loading..." );
last.setEnabled(web.canGoBack());
next.setEnabled(web.canGoForward());
super .onPageStarted(view,
url, favicon); }
}; /** *
对WebView的实现其中 WebChromeClient的方法 */ WebChromeClient
chromeClient= new
WebChromeClient(){ //实现进度条 @Override public
void
onProgressChanged(WebView view, int
newProgress) { //
TODO Auto-generated method stub super .onProgressChanged(view,
newProgress); progress.setProgress(newProgress); } //对历史记录的刷新 @Override public
void
getVisitedHistory(ValueCallback<String[]> callback) { //
TODO Auto-generated method stub super .getVisitedHistory(callback); } }; //实现Button的按钮的动作 public
class
Action implements
OnClickListener{ @Override public
void
onClick(View v) { //
TODO Auto-generated method stub switch (v.getId()){ case
R.id.go: WebMainActivity. this .web.loadUrl(WebMainActivity. this .input.getText().toString().trim()); WebMainActivity. this .input.setText( "http://" ); break ; case
R.id.last: if (WebMainActivity. this .web.canGoBack()) WebMainActivity. this .web.goBack(); else v.setClickable( false ); break ; case
R.id.next: if (WebMainActivity. this .web.canGoForward()) WebMainActivity. this .web.goForward(); else v.setClickable( false ); break ; case
R.id.history: //跳转到History页面 Intent
intent= new
Intent(getApplicationContext(),History. class ); WebMainActivity. this .startActivityForResult(intent,
1 ); break ; default : break ; } } } /** *
实现触摸事件 */ @Override public
boolean
onTouchEvent(MotionEvent event) { //
TODO Auto-generated method stub if (event.getHistorySize()== 2 ) switch (event.getAction()) { case
MotionEvent.ACTION_DOWN: WebMainActivity.beforeLenght=Spacing(event); case
MotionEvent.ACTION_UP: WebMainActivity.changeLenght=Spacing(event); default : break ; } return
super .onTouchEvent(event); } /** *
计算两指的距离 *
@param event *
@return */ private
float
Spacing(MotionEvent event) { float
x = event.getX( 0 )
- event.getX( 1 ); float
y = event.getY( 0 )
- event.getY( 1 ); return
FloatMath.sqrt(x * x + y * y); } //findViewById实现布局中的控件 public
void
findView(){ go=(Button)findViewById(R.id.go); next=(Button)findViewById(R.id.next); last=(Button)findViewById(R.id.last); history=(Button)findViewById(R.id.history); input=(EditText)findViewById(R.id.inputText); web=(WebView)findViewById(R.id.web); progress=(ProgressBar)findViewById(R.id.progress); } //得到历史信息写入history.txt public
void
getHistory(String url){ FileOutputStream
fos = null ; try
{ fos=openFileOutput(FILE_NAME,
MODE_APPEND); try
{ fos.write((url+ ";" ).getBytes()); }
catch
(IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); } }
catch
(FileNotFoundException e) { //
TODO Auto-generated catch block e.printStackTrace(); } finally { if (fos!= null ){ try
{ fos.flush(); fos.close(); }
catch
(IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); } } } } //得到网页的标题写入title.txt public
void
getTitles(String url){ FileOutputStream
fos = null ; try
{ fos=openFileOutput( "title.txt" ,
MODE_APPEND); try
{ fos.write((url+ ";" ).getBytes()); }
catch
(IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); } }
catch
(FileNotFoundException e) { //
TODO Auto-generated catch block e.printStackTrace(); } finally { if (fos!= null ){ try
{ fos.flush(); fos.close(); }
catch
(IOException e) { //
TODO Auto-generated catch block e.printStackTrace(); } } } } /** *
对History的Activity的返回信息进行操作 */ @Override protected
void
onActivityResult( int
requestCode, int
resultCode, Intent data) { //
TODO Auto-generated method stub switch (resultCode){ case
RESULT_OK: web.loadUrl(data.getStringExtra( "retmsg" )); break ; case
RESULT_CANCELED: break ; default : break ; } } //对返回键的重写 @Override public
boolean
onKeyDown( int
keyCode, KeyEvent event) { //
TODO Auto-generated method stub if (keyCode==KeyEvent.KEYCODE_BACK){ if (web.canGoBack()){ web.goBack(); return
true ; } else { System.exit( 0 ); } } return
super .onKeyDown(keyCode,
event); } @Override protected
void
onPause() { //
TODO Auto-generated method stub web.pauseTimers(); if (isFinishing()){ web.loadUrl( "about:blank" ); setContentView( new
FrameLayout( this )); } super .onPause(); } @Override protected
void
onResume() { //
TODO Auto-generated method stub super .onResume(); web.resumeTimers(); } @Override public
boolean
onCreateOptionsMenu(Menu menu) { //
Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.web_main,
menu); return
true ; } } |
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
|
< 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" tools:ignore = "WebViewLayout,TextFields,HardcodedText,InefficientWeight,ButtonStyle"
> < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "horizontal" > < Button android:id = "@+id/go" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/go"
/> < EditText android:id = "@+id/inputText" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "http://" /> </ LinearLayout > < ProgressBar android:id = "@+id/progress" style = "?android:attr/progressBarStyleHorizontal" android:layout_width = "fill_parent" android:layout_height = "wrap_content"
/> < FrameLayout android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:layout_weight = "1"
> < WebView android:id = "@+id/web" android:layout_width = "wrap_content" android:layout_height = "wrap_content"
/> </ FrameLayout > < LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" android:orientation = "horizontal" > < Button android:id = "@+id/last" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/last"
/> < Button android:id = "@+id/next" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/next"
/> < Button android:id = "@+id/history" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/history"
/> </ LinearLayout > </ LinearLayout > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? xml
version = "1.0"
encoding = "utf-8" ?> < LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical"
> < TextView android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textSize = "15pt" android:text = "TextView"
/> < TextView android:id = "@+id/textView2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:textSize = "5pt" android:text = "TextView"
/> </ LinearLayout > |
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
|
<? xml
version = "1.0"
encoding = "utf-8" ?> < LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:ignore = "ButtonStyle,InefficientWeight"
> < FrameLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_weight = "1"
> < ListView android:id = "@+id/historylist" android:layout_width = "fill_parent" android:layout_height = "wrap_content" > </ ListView > </ FrameLayout > < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:gravity = "center" android:orientation = "horizontal"
> < Button android:id = "@+id/back" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/back"
/> < Button android:id = "@+id/clear" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "@string/clear"
/> </ LinearLayout > </ LinearLayout > |
标签:
原文地址:http://blog.csdn.net/u014311051/article/details/42283121