<p class="agreement" οnclick="agree();"><span class="icon-agree" id="agree-btn"></span>我已阅读并同意<a href="http://www.dahuo.la/dahuo-private-equity-tos.html" class="risk-warning-letter">《风险提示书》</a></p>
</div>
<script>
function agree()
{
var _agreeBtn = document.getElementById("agree-btn")
var className = _agreeBtn.className;
if(className == 'icon-disagree'){
_agreeBtn.className = 'icon-agree';
dahuo.agreeLicense();
} else {
_agreeBtn.className = 'icon-disagree';
dahuo.disagreeLicense();
}
}
</script>
String content = ResourcesManager.loadAssetFile("subscription_xuzhi.html");
TextView submitBtn = (TextView) findViewById(R.id.submit_btn);
mWebView = (WebView) findViewById(R.id.webview);
final WebSettings webSettings = mWebView.getSettings();
initWebSettings(webSettings);
mWebView.addJavascriptInterface(new CFSInvestNoticeJsCallback(submitBtn), Constants.JS_BRIDGE_NAME);
mWebView.setWebViewClient(new WebViewClient() {
});
mWebView.setSaveEnabled(false);
mWebView.setWebChromeClient(new WebChromeClient(){
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder builder = new AlertDialog.Builder(CFSInvestNoticeActivity.this);
builder.setMessage(message);
builder.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface arg0) {
result.confirm();
}
});
builder.show();
return true;
}
});
mWebView.loadUrl(content);
@KeepAll
public static final class CFSInvestNoticeJsCallback {
CFSInvestNoticeView view;
CFSInvestNoticeJsCallback(final CFSInvestNoticeView v) {
view = v;
}
@JavascriptInterface
public void agreeLicense() {
view.handleAgreeProtocolClick();
}
@JavascriptInterface
public void disagreeLicense() {
view.handleAgreeProtocolClick();
}
}
总结
1、方法前要打标记JavascriptInterface
2、申明callback类最上面加入
@SuppressLint({
"JavascriptInterface", "SetJavaScriptEnabled"
})
3、注意webView的loadUrl方法调用必须要在addJavascriptInterface后面否则不生效