当在Android的webview中设置内置缩放控件为true时,ontouch不起作用



我正在Android中处理webview.我的代码如下

private final Handler handler = new Handler(this);
private WebView wv;

wv.getSettings().setBuiltInZoomControls(true);
wv = (WebView) v.findViewById(R.id.webView1);
            String data = "<html><link rel="stylesheet" type="text/css" href="style.css" />"
                    + "<meta name="viewport" content="width=device-width; initial-scale=1.0; min-scale=1.0; max-scale=3.0; " />"
                    + "<body>"
                    + "<div style="text-align:center;width:100&#37;">"
                    + "Murti"
                    + "</div> <br/><div>"
                    + "Touch and hold one of the customizable icons, then drag it to Remove. Touch ... The controls appear under your finger, and you can slide to a control and select it. ... Find apps with the biggest Wi-Fi data usage, then reduce their sync"
                    + "</div><br/><br/><br/><br/></body></html>";
            wv.loadDataWithBaseURL("file:///android_asset/", data,
                    "text/html", "UTF-8", null);
wv.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    if (v.getId() == R.id.webView1
                            && event.getAction() == MotionEvent.ACTION_DOWN) {
                        handler.sendEmptyMessageDelayed(1, 500);
                    }
                    return false;
                }
            });
public boolean handleMessage(Message msg) {
    if (msg.what == 1) {
        Toast.makeText(MainActivity.this, "WebView clicked",
                Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}

Isuue 是当我在 WebView 上捏缩放比缩放控制可见,之后 ontouch 不起作用。

删除OnTouchListener并使用 dispatchTouchEvent

代替

http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)http://developer.android.com/reference/android/view/ViewGroup.html#dispatchTouchEvent(android.view.MotionEvent)http://developer.android.com/reference/android/view/ViewGroup.html#setDescendantFocusability(整型)

创建自己的类来扩展WebView并覆盖dispatchTouchEvent(MotionEvent ev)

public class MyWebView extend WebView {
    //constructor 
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // code
        return super.dispatchTouchEvent(ev);
    }
}; 

如果您想了解更多关于调度器的信息,欢迎在这里如何使用调度触摸事件和这里,onintercepttouchevent 和ACTION_DOWN等等......

最新更新