I have the following html code loaded in a webview:
<form>
<input checked="true" id="myCheckBox" onchange=
"location.href='/doSomething.html'" type=
"checkbox">My Checkbox description
</form>
I can not modify this the html code because I'm not the owner of the web site. What I want to do is to receive in my Android app if the user clicks on the checkbox, and the value of the checkbox (checked or not).
I need to manipulate the state of the check box too, but this was easy.
I'm using the following code:
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JsObject(), "injection");
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.loadUrl(
"javascript:(function() { " +
"var ch=document.getElementById('myCheckBox');" +
"ch.checked=false; " + //This works!!!
"injection.reportCheckboxState('111111');" + //This works too
"function myFunction() {" +
" injection.reportCheckboxState('222222'); " +
"}"+
"ch.onClick=myFunction;"+
//"ch.addEventListener(\"onClick\", myFunction);"+
"})()"
);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean returnVal = false;
if (url.equalsIgnoreCase("/doSomething.htm")) {
//I can intercept the onClick event on this checkbox, but I don't know how to read the value of the checkbox here
} else {
returnVal = true;
}
return returnVal;
}
});
private class JsObject {
@JavascriptInterface
public void reportCheckboxState(String string) {
Log.d(TAG, "" + string);
}
}
What I'm trying to do is to inject some Javascript into this HTML form. The first lines of the code are working right:
"var ch=document.getElementById('myCheckBox');" +
"ch.checked=false; " +
"injection.reportCheckboxState('111111');"
I receive in my android log console the string "111111". And the ch.cheched=false works too.
But I'm unable to instruct the checkbox that it should call the injection.reportCheckboxState method on "onClick" event (when the user touches the checkbox)".
Can anybody help me? Is it possible to do this?
Thanks ind advance.
Aucun commentaire:
Enregistrer un commentaire