2、MainWebView3.java的内容,请注意注释内容:
001 | package android.basic.lesson29; |
003 | import java.io.FileOutputStream; |
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; |
023 | public class MainWebView3 extends Activity { |
025 | /** Called when the activity is first created. */ |
027 | public void onCreate(Bundle savedInstanceState) { |
028 | super .onCreate(savedInstanceState); |
029 | setContentView(R.layout.main); |
032 | Button b1 = (Button) findViewById(R.id.Button01); |
033 | Button b2 = (Button) findViewById(R.id.Button02); |
034 | Button b3 = (Button) findViewById(R.id.Button03); |
036 | final WebView wv = (WebView) findViewById(R.id.WebView01); |
039 | wv.setOnKeyListener( new View.OnKeyListener() { |
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()) { |
054 | wv.getSettings().setJavaScriptEnabled( true ); |
057 | b1.setOnClickListener( new View.OnClickListener() { |
059 | public void onClick(View v) { |
066 | b2.setOnClickListener( new View.OnClickListener() { |
068 | public void onClick(View v) { |
075 | b3.setOnClickListener( new View.OnClickListener() { |
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); |
086 | String fileName = "sdcard/" + System.currentTimeMillis() + ".png" ; |
087 | FileOutputStream fos = new FileOutputStream(fileName); |
090 | bmp.compress(Bitmap.CompressFormat.PNG, 90 , fos); |
093 | Toast.makeText(getApplicationContext(), "截图成功,文件名是:" + fileName, Toast.LENGTH_SHORT).show(); |
094 | } catch (Exception e) { |
102 | WebViewClient wvc = new WebViewClient() { |
105 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
106 | Toast.makeText(getApplicationContext(), "WebViewClient.shouldOverrideUrlLoading" , Toast.LENGTH_SHORT) |
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); |
121 | public void onPageFinished(WebView view, String url) { |
122 | Toast.makeText(getApplicationContext(), "WebViewClient.onPageFinished" , Toast.LENGTH_SHORT).show(); |
123 | super .onPageFinished(view, url); |
127 | public void onLoadResource(WebView view, String url) { |
128 | Toast.makeText(getApplicationContext(), "WebViewClient.onLoadResource" , Toast.LENGTH_SHORT).show(); |
129 | super .onLoadResource(view, url); |
135 | wv.setWebViewClient(wvc); |
138 | WebChromeClient wvcc = new WebChromeClient() { |
142 | public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { |
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() { |
149 | public void onClick(DialogInterface dialog, int which) { |
153 | builder.setCancelable( false ); |
160 | public void onReceivedTitle(WebView view, String title) { |
161 | MainWebView3. this .setTitle( "可以用onReceivedTitle()方法修改网页标题" ); |
162 | super .onReceivedTitle(view, title); |
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() { |
174 | public void onClick(DialogInterface dialog, int which) { |
179 | builder.setNeutralButton(android.R.string.cancel, new AlertDialog.OnClickListener() { |
182 | public void onClick(DialogInterface dialog, int which) { |
187 | builder.setCancelable( false ); |
195 | public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, |
196 | JsPromptResult result) { |
198 | return super .onJsPrompt(view, url, message, defaultValue, result); |
203 | wv.setWebChromeClient(wvcc); |
3、运行程序,查看结果:
![image image](http://android.yaohuiji.com/wp-content/uploads/2010/08/image_thumb41.png)
启动起来时的画面
![image image](http://android.yaohuiji.com/wp-content/uploads/2010/08/image_thumb42.png)
点击第一个按钮,我们可以看到WebViewClient对象中定义的方法的确被调用了。
点击第二个按钮,我们看到加载的网页中有两个按钮,我们知道默认情况下Android会直接忽略掉由javascript弄出来的alert等弹出信息,除非我们在WebChromeClient中覆盖onJsAlert()方法和onJsConfirm()方法,让我们分别点击它们看看效果。
![image image](http://android.yaohuiji.com/wp-content/uploads/2010/08/image_thumb44.png)
![image image](http://android.yaohuiji.com/wp-content/uploads/2010/08/image_thumb45.png)
接下来我们再页面中跳转几次后,使用后退按钮
,查看我们是否屏蔽了Activity默认的行为,结果当然是屏蔽了^_^,这一段代码没有留意的同学回去在看一下吧。
接下来我们把页面切换到这一个网页:
![image image](http://android.yaohuiji.com/wp-content/uploads/2010/08/image_thumb46.png)
然后我们按下第三个保存图片的按钮,不出意外的话我们截取到了该页的完整图片,并把它保存到了sdcard中了,我在这里展示一下我截取的效果给你看,别被它清晰的效果吓住就行^_^
![1282569949780 1282569949780](http://android.yaohuiji.com/wp-content/uploads/2010/08/1282569949780_thumb.png)
好了,因为篇幅关系,我们关于WebView的功能也只能介绍到这里,关于WebView的其它功能我们在接下来的项目中会涉及到,到时候我们再一起学习,本讲就到这里吧,各位下次再见。