当android中的webview中的setBuiltInZoomControls为true时,ontouch不起作用

时间:2012-12-14 05:58:19

标签: android webview

我在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不工作。没有缩放ontouch工作正常。

1 个答案:

答案 0 :(得分:0)

删除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(int)

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

public class MyWebView extend WebView {

    //constructor 

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // code
        return super.dispatchTouchEvent(ev);
    }
}; 

如果您想了解有关调度员的更多信息,请访问How to use dispatchtoucheventhereonintercepttouchevent and ACTION_DOWN等等...