2、MainWebView3.java的內容,請注意註釋內容:
001 |
package android.basic.lesson29; |
002 |
003 |
import java.io.FileOutputStream; |
004 |
005 |
import android.app.Activity; |
006 |
import android.app.AlertDialog; |
007 |
import android.app.AlertDialog.Builder; |
008 |
import android.content.DialogInterface; |
009 |
import android.graphics.Bitmap; |
010 |
import android.graphics.Canvas; |
011 |
import android.graphics.Picture; |
012 |
import android.os.Bundle; |
013 |
import android.view.KeyEvent; |
014 |
import android.view.View; |
015 |
import android.webkit.JsPromptResult; |
016 |
import android.webkit.JsResult; |
017 |
import android.webkit.WebChromeClient; |
018 |
import android.webkit.WebView; |
019 |
import android.webkit.WebViewClient; |
020 |
import android.widget.Button; |
021 |
import android.widget.Toast; |
022 |
023 |
public class MainWebView3 extends Activity
{ |
024 |
025 |
/**
Called when the activity is first created. */ |
026 |
@Override |
027 |
public void onCreate(Bundle
savedInstanceState) { |
028 |
super .onCreate(savedInstanceState); |
029 |
setContentView(R.layout.main); |
030 |
031 |
//
定義UI組件 |
032 |
Button
b1 = (Button) findViewById(R.id.Button01); |
033 |
Button
b2 = (Button) findViewById(R.id.Button02); |
034 |
Button
b3 = (Button) findViewById(R.id.Button03); |
035 |
036 |
final WebView
wv = (WebView) findViewById(R.id.WebView01); |
037 |
038 |
//
覆蓋默認後退按鈕的作用,替換成WebView裏的查看歷史頁面 |
039 |
wv.setOnKeyListener( new View.OnKeyListener()
{ |
040 |
041 |
@Override |
042 |
public boolean onKey(View
v, int keyCode,
KeyEvent event) { |
043 |
if (event.getAction()
== KeyEvent.ACTION_DOWN) { |
044 |
if ((keyCode
== KeyEvent.KEYCODE_BACK) && wv.canGoBack()) { |
045 |
wv.goBack(); |
046 |
return true ; |
047 |
} |
048 |
} |
049 |
return false ; |
050 |
} |
051 |
}); |
052 |
053 |
//
設置支持Javascript |
054 |
wv.getSettings().setJavaScriptEnabled( true ); |
055 |
056 |
//
定義並綁定按鈕單擊監聽器 |
057 |
b1.setOnClickListener( new View.OnClickListener()
{ |
058 |
@Override |
059 |
public void onClick(View
v) { |
060 |
//
加載URL assets目錄下的內容可以用 "file:///android_asset"
前綴 |
061 |
wv.loadUrl( "file:///android_asset/html/test1.html" ); |
062 |
} |
063 |
}); |
064 |
065 |
//
定義並綁定按鈕單擊監聽器 |
066 |
b2.setOnClickListener( new View.OnClickListener()
{ |
067 |
@Override |
068 |
public void onClick(View
v) { |
069 |
//
加載URL assets目錄下的內容可以用 "file:///android_asset"
前綴 |
070 |
wv.loadUrl( "file:///android_asset/html/test3.html" ); |
071 |
} |
072 |
}); |
073 |
074 |
//
定義並綁定按鈕單擊監聽器 |
075 |
b3.setOnClickListener( new View.OnClickListener()
{ |
076 |
@Override |
077 |
public void onClick(View
v) { |
078 |
Picture
pic = wv.capturePicture(); |
079 |
int width
= pic.getWidth(); |
080 |
int height
= pic.getHeight(); |
081 |
if (width
> 0 &&
height > 0 )
{ |
082 |
Bitmap
bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
083 |
Canvas
canvas = new Canvas(bmp); |
084 |
pic.draw(canvas); |
085 |
try { |
086 |
String
fileName = "sdcard/" +
System.currentTimeMillis() + ".png" ; |
087 |
FileOutputStream
fos = new FileOutputStream(fileName); |
088 |
089 |
if (fos
!= null )
{ |
090 |
bmp.compress(Bitmap.CompressFormat.PNG, 90 ,
fos); |
091 |
fos.close(); |
092 |
} |
093 |
Toast.makeText(getApplicationContext(), "截圖成功,文件名是:" +
fileName, Toast.LENGTH_SHORT).show(); |
094 |
} catch (Exception
e) { |
095 |
e.printStackTrace(); |
096 |
} |
097 |
} |
098 |
} |
099 |
}); |
100 |
101 |
//
創建WebViewClient對象 |
102 |
WebViewClient
wvc = new WebViewClient()
{ |
103 |
104 |
@Override |
105 |
public boolean shouldOverrideUrlLoading(WebView
view, String url) { |
106 |
Toast.makeText(getApplicationContext(), "WebViewClient.shouldOverrideUrlLoading" ,
Toast.LENGTH_SHORT) |
107 |
.show(); |
108 |
//
使用自己的WebView組件來響應Url加載事件,而不是使用默認瀏覽器器加載頁面 |
109 |
wv.loadUrl(url); |
110 |
//
記得消耗掉這個事件。給不知道的朋友再解釋一下,Android中返回True的意思就是到此爲止吧,事件就會不會冒泡傳遞了,我們稱之爲消耗掉 |
111 |
return true ; |
112 |
} |
113 |
114 |
@Override |
115 |
public void onPageStarted(WebView
view, String url, Bitmap favicon) { |
116 |
Toast.makeText(getApplicationContext(), "WebViewClient.onPageStarted" ,
Toast.LENGTH_SHORT).show(); |
117 |
super .onPageStarted(view,
url, favicon); |
118 |
} |
119 |
120 |
@Override |
121 |
public void onPageFinished(WebView
view, String url) { |
122 |
Toast.makeText(getApplicationContext(), "WebViewClient.onPageFinished" ,
Toast.LENGTH_SHORT).show(); |
123 |
super .onPageFinished(view,
url); |
124 |
} |
125 |
126 |
@Override |
127 |
public void onLoadResource(WebView
view, String url) { |
128 |
Toast.makeText(getApplicationContext(), "WebViewClient.onLoadResource" ,
Toast.LENGTH_SHORT).show(); |
129 |
super .onLoadResource(view,
url); |
130 |
} |
131 |
132 |
}; |
133 |
134 |
//
設置WebViewClient對象 |
135 |
wv.setWebViewClient(wvc); |
136 |
137 |
//
創建WebViewChromeClient |
138 |
WebChromeClient
wvcc = new WebChromeClient()
{ |
139 |
140 |
//
處理Alert事件 |
141 |
@Override |
142 |
public boolean onJsAlert(WebView
view, String url, String message, final JsResult
result) { |
143 |
//
構建一個Builder來顯示網頁中的alert對話框 |
144 |
Builder
builder = new Builder(MainWebView3. this ); |
145 |
builder.setTitle( "計算1+2的值" ); |
146 |
builder.setMessage(message); |
147 |
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener()
{ |
148 |
@Override |
149 |
public void onClick(DialogInterface
dialog, int which)
{ |
150 |
result.confirm(); |
151 |
} |
152 |
}); |
153 |
builder.setCancelable( false ); |
154 |
builder.create(); |
155 |
builder.show(); |
156 |
return true ; |
157 |
} |
158 |
159 |
@Override |
160 |
public void onReceivedTitle(WebView
view, String title) { |
161 |
MainWebView3. this .setTitle( "可以用onReceivedTitle()方法修改網頁標題" ); |
162 |
super .onReceivedTitle(view,
title); |
163 |
} |
164 |
165 |
//
處理Confirm事件 |
166 |
@Override |
167 |
public boolean onJsConfirm(WebView
view, String url, String message, final JsResult
result) { |
168 |
Builder
builder = new Builder(MainWebView3. this ); |
169 |
builder.setTitle( "刪除確認" ); |
170 |
builder.setMessage(message); |
171 |
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener()
{ |
172 |
173 |
@Override |
174 |
public void onClick(DialogInterface
dialog, int which)
{ |
175 |
result.confirm(); |
176 |
} |
177 |
178 |
}); |
179 |
builder.setNeutralButton(android.R.string.cancel, new AlertDialog.OnClickListener()
{ |
180 |
181 |
@Override |
182 |
public void onClick(DialogInterface
dialog, int which)
{ |
183 |
result.cancel(); |
184 |
} |
185 |
186 |
}); |
187 |
builder.setCancelable( false ); |
188 |
builder.create(); |
189 |
builder.show(); |
190 |
return true ; |
191 |
} |
192 |
193 |
//
處理提示事件 |
194 |
@Override |
195 |
public boolean onJsPrompt(WebView
view, String url, String message, String defaultValue, |
196 |
JsPromptResult
result) { |
197 |
//
看看默認的效果 |
198 |
return super .onJsPrompt(view,
url, message, defaultValue, result); |
199 |
} |
200 |
}; |
201 |
202 |
//
設置setWebChromeClient對象 |
203 |
wv.setWebChromeClient(wvcc); |
204 |
} |
205 |
} |
3、運行程序,查看結果:
啓動起來時的畫面
點擊第一個按鈕,我們可以看到WebViewClient對象中定義的方法的確被調用了。
點擊第二個按鈕,我們看到加載的網頁中有兩個按鈕,我們知道默認情況下Android會直接忽略掉由javascript弄出來的alert等彈出信息,除非我們在WebChromeClient中覆蓋onJsAlert()方法和onJsConfirm()方法,讓我們分別點擊它們看看效果。
接下來我們再頁面中跳轉幾次後,使用後退按鈕,查看我們是否屏蔽了Activity默認的行爲,結果當然是屏蔽了^_^,這一段代碼沒有留意的同學回去在看一下吧。
然後我們按下第三個保存圖片的按鈕,不出意外的話我們截取到了該頁的完整圖片,並把它保存到了sdcard中了,我在這裏展示一下我截取的效果給你看,別被它清晰的效果嚇住就行^_^
好了,因爲篇幅關係,我們關於WebView的功能也只能介紹到這裏,關於WebView的其它功能我們在接下來的項目中會涉及到,到時候我們再一起學習,本講就到這裏吧,各位下次再見。