开发者

detect javascript button press in webview?

开发者 https://www.devze.com 2023-03-10 01:12 出处:网络
Does开发者_运维问答 anyone know a way to detect if a button with this, code below, in the source of the webpage is pressed in my apps webview?

Does开发者_运维问答 anyone know a way to detect if a button with this, code below, in the source of the webpage is pressed in my apps webview?

html source of button:

<input id="testBtn" name="testBtn" type="button" value="Test Button" 
class="button" onclick="test.run(this.id,'onclick');return false;" 
onkeydown="test.run(this.id,'onkeydown');return false;" title="Test Button">

and then make a toast?

P.s. I cant change html code it is not my site


that will be easy just define your Javascript Interface

final class jocJavaScriptInterface {
    jocJavaScriptInterface() {
    }
    String myEvent;
    public void run(int id,String event) { //run method defined into your html.
        this.myEvent = event;
        mHandler.post(new Runnable() {
            public void run() {
        Toast.makeText(getApplicationContext(),myEvent + " my ButtoN!", Toast.LENGTH_LONG).show();
            }
        });
    }
}

Add the Javascript Interface to you webview

    WebContent.addJavascriptInterface(new jocJavaScriptInterface(), "test");        
    WebContent.loadUrl("http://www.foo.com/Androidtest.html"); //load your Page with the button...
    WebSettings webSettings = WebContent.getSettings();
    webSettings.setSavePassword(false);
    webSettings.setSaveFormData(false);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(true);     
0

精彩评论

暂无评论...
验证码 换一张
取 消