WebChromeClient////WebViewClient区别
2016-08-19 20:23
441 查看
A WebView has several customization points where you can add your own behavior. These are:
Creating and setting a
for instance, progress updates and JavaScript alerts are sent here (see
Debugging Tasks).
Creating and setting a
eg, errors or form submissions. You can also intercept URL loading here (via
Modifying the
Injecting Java objects into the WebView using the
WebvIew有一些自己定义的点,你可以加入自己想要的行为:
1. 建立并设置WebChromeClient的子类。当影响浏览器UI内容的事件发生时,这个类就会被调用,例如,进度更新以及,Javascript的alert都在这里传入。
2.建立并设置WebViewClient子类。当影响内容的事件发生时,这个类会被调用,例如错误或是表单提交。也可以在这个类里拦截url加载(复写shouldOverrideUrlLoding()方法)
3.修改WebSetting,例如,设置允许javasc交互,调用方法seetJavaScriptEnabled();
4.利用addjavascriptinterface(object,string)()方法向WebView传入一个java对象。这个方法允许你向javascript环境中传入java对象,这样就可以在界面中通过javascript访问这个对象。
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new Object() {
public void clickOnAndroid() {
mHandler.post(new Runnable() {
public void run() {
mWebView.loadUrl("javascript:wave()"); //
可以利用这个方法直接webview调用javascript
}
});
}
}, "demo"); //设置给javascript调用
mWebView.loadUrl("file:///android_asset/demo.html"); //
可以利用这个方法直接webview调用javascript
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack(); ////返回到上个界面
return true;
}
return super.onKeyDown(keyCode, event);
}
<html>
<mce:script language="javascript"><!--
function wave() {
document.getElementById("droid").src="android_waving.png";
}
// --></mce:script>
<body>
<a onClick="window.demo.clickOnAndroid()"> ////javascript调用java中的方法demo是
在addjavascriptInterface中设置的
<img id="droid" src="android_normal.png" mce_src="android_normal.png"/><br>
Click me!
</a>
</body>
</html>
A WebView has several customization points where you can add your own behavior. These are:
Creating and setting a
WebChromeClientsubclass. This class is called when something that might impact a browser UI happens,
for instance, progress updates and JavaScript alerts are sent here (see
Debugging Tasks).
Creating and setting a
WebViewClientsubclass. It will be called when things happen that impact the rendering of the content,
eg, errors or form submissions. You can also intercept URL loading here (via
shouldOverrideUrlLoading()).
Modifying the
WebSettings, such as enabling JavaScript with
setJavaScriptEnabled().
Injecting Java objects into the WebView using the
addJavascriptInterface(Object, String)method. This method allows you to inject Java objects into a page's JavaScript context, so that they can be accessed by JavaScript in the page.
WebvIew有一些自己定义的点,你可以加入自己想要的行为:
1. 建立并设置WebChromeClient的子类。当影响浏览器UI内容的事件发生时,这个类就会被调用,例如,进度更新以及,Javascript的alert都在这里传入。
2.建立并设置WebViewClient子类。当影响内容的事件发生时,这个类会被调用,例如错误或是表单提交。也可以在这个类里拦截url加载(复写shouldOverrideUrlLoding()方法)
3.修改WebSetting,例如,设置允许javasc交互,调用方法seetJavaScriptEnabled();
4.利用addjavascriptinterface(object,string)()方法向WebView传入一个java对象。这个方法允许你向javascript环境中传入java对象,这样就可以在界面中通过javascript访问这个对象。
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new Object() {
public void clickOnAndroid() {
mHandler.post(new Runnable() {
public void run() {
mWebView.loadUrl("javascript:wave()"); //
可以利用这个方法直接webview调用javascript
}
});
}
}, "demo"); //设置给javascript调用
mWebView.loadUrl("file:///android_asset/demo.html"); //
可以利用这个方法直接webview调用javascript
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack(); ////返回到上个界面
return true;
}
return super.onKeyDown(keyCode, event);
}
<html>
<mce:script language="javascript"><!--
function wave() {
document.getElementById("droid").src="android_waving.png";
}
// --></mce:script>
<body>
<a onClick="window.demo.clickOnAndroid()"> ////javascript调用java中的方法demo是
在addjavascriptInterface中设置的
<img id="droid" src="android_normal.png" mce_src="android_normal.png"/><br>
Click me!
</a>
</body>
</html>
相关文章推荐
- WebViewClient与WebChromeClient的区别
- Android之WebViewClient与WebChromeClient的区别
- WebViewClient与WebChromeClient的区别
- Android之WebViewClient与WebChromeClient的区别
- Android中WebChromeClient和WebViewClient的区别浅析
- Android之WebViewClient与WebChromeClient的区别
- WebViewClient与WebChromeClient的区别
- WebViewClient与WebChromeClient的区别
- WebViewClient与WebChromeClient的区别
- WebChromeClient 与WebViewClient的区别
- WebView,WebViewClient,WebChromeClient区别(转载)
- Android之WebViewClient与WebChromeClient的区别
- WebViewClient与WebChromeClient的区别
- Android中WebView,WebViewClient,WebChromeClient区别
- WebViewClient与WebChromeClient 区别
- Android之WebViewClient与WebChromeClient的区别
- Android中WebView方法WebViewClient和WebChromeClient的区别
- WebViewClient与WebChromeClient的区别
- WebViewClient与WebChromeClient的区别
- Android中WebView,WebViewClient,WebChromeClient区别