WebView模拟单击

时间:2012-11-28 15:50:14

标签: android android-webview

我有一个自定义WebView,我希望在获得MotionEvent.ACTION_DOWN时模拟一次点击。我不想在WebView上有任何类型的输入,我甚至不能使它可点击(.setClickable(false))。所以我所做的是覆盖自定义WebView中的onTouchEvent()以返回false。

这工作正常,但我错过了点击。我在WebView的源代码中看到它将消息发送到名为WebViewCore的类,但是在每个版本的android中这种通信都是以不同的方式完成的。

有谁知道如何以编程方式将点击发送到webView?

这是我正在尝试做的事情:

public class VoidWebView extends WebView {


    public VoidWebView(Context context) {
        super(context);

    }
    public VoidWebView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
                //tell the webview that a click has been performed , it doesn't matter where the click happened

        }
        return false;
    }


}

2 个答案:

答案 0 :(得分:3)

我认为您正在寻找View

的performClick()方法

您可以获得更多信息here

答案 1 :(得分:0)

将上下事件传递给超级课程。

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    if(action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP){
         super.onTouchEvent(event);
         return true;
    }
    return false;
}