Android:webView总结
2010-09-10 01:03:00 来源:WEB开发网3. mWebView.goBack();
4. return true;
5. }
6. return super.onKeyDown(keyCode, event);
7. }
public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); }
下一步让我们来了解一下android中webview是如何支持javascripte自定义对象的,在w3c标准中js有 window,history,document等标准对象,同样我们可以在开发浏览器时自己定义我们的对象调用手机系统功能来处理,这样使用js就可以为所欲为了。
看一个实例:
view plaincopy to clipboardprint?
1. public class WebViewDemo extends Activity {
2. private WebView mWebView;
3. private Handler mHandler = new Handler();
4.
5. public void onCreate(Bundle icicle) {
6. super.onCreate(icicle);
7. setContentView(R.layout.webviewdemo);
8. mWebView = (WebView) findViewById(R.id.webview);
9. WebSettings webSettings = mWebView.getSettings();
10. webSettings.setJavaScriptEnabled(true);
11. mWebView.addJavascriptInterface(new Object() {
12. public void clickOnAndroid() {
13. mHandler.post(new Runnable() {
14. public void run() {
15. mWebView.loadUrl("javascript:wave()");
16. }
17. });
18. }
19. }, "demo");
20. mWebView.loadUrl("file:///android_asset/demo.html");
21. }
22. }
public class WebViewDemo extends Activity { private WebView mWebView; private Handler mHandler = new Handler(); public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.webviewdemo); mWebView = (WebView) findViewById(R.id.webview); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mWebView.addJavascriptInterface(new Object() { public void clickOnAndroid() {
更多精彩
赞助商链接