在webview中检测javascript按钮?

时间:2011-05-31 21:51:35

标签: android html webview

有没有人知道如何在我的应用网页视图中检测网页来源中是否按下了具有此代码的按钮?

html按钮源:

<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">

然后祝酒?

P.S。我不能改变HTML代码它不是我的网站

1 个答案:

答案 0 :(得分:0)

这将很容易定义您的Javascript接口

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();
            }
        });
    }
}

将Javascript界面​​添加到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);