Web视图仅在未单击链接的情况下触摸检测android

Web视图仅在未单击链接的情况下触摸检测android

本文介绍了Web视图仅在未单击链接的情况下触摸检测android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载静态htmls& webview中的网址.另外,我的网页视图是可点击的,其点击意图已被触发.

I load static htmls & urls in webview. Also, my web view is clickable ,on its click intent is fired.

我需要在检测到Webview&上的触摸时实施onTouch侦听器开火意图.但是,如果我实现了ontouch,则内部链接将永远不会在webview的html/url中打开

public class WebViewHotspotClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
//        return super.shouldOverrideUrlLoading(view, url);
//        return true; does not open link
        return false;//opens link in web view
    }
}

    hotspotWebview.setWebViewClient(new WebViewHotspotClient());
//        hotspotWebview.setOnTouchListener(new OnTouchListener() {
//            @Override
//            public boolean onTouch(View view, MotionEvent motionEvent) {
//                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
//                    startActivity(new Intent(ImageFrameActivity.this, HotspotActivity.class));
//                }
//                return false;
//            }
//        });

谢谢!

推荐答案

这里发生的是,当您将ontouchlistener分配给整个Web视图时,其他元素位于其下方,而它们没有任何触摸或点击.要解决此问题,您可以按照

What is happening here is when you assign ontouchlistener to the whole webview other elements go under it and they dont get any touch or click. To solve this problem you can do following

public class MyWebViewClicker extends Activity implements OnTouchListener,     Handler.Callback {

private static final int CLICK_WEBVIEW = 1;
private static final int CLICK_URL = 2;

private final Handler handler = new Handler(this);

private WebView webView;
private WebViewClient webviewclient;
boolean donext=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);

webView = (WebView)findViewById(R.id.myweb);
webView.setOnTouchListener(this);

webviewclient = new WebViewClient(){
    @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
        handler.sendEmptyMessage(CLICK_URL);
        return false;
    }
};

webView.setWebViewClient(webviewclient);
webView.setVerticalScrollBarEnabled(false);
webView.loadUrl("http://www.example.com");
}

@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.myweb && event.getAction() == MotionEvent.ACTION_DOWN)  {
    handler.sendEmptyMessageDelayed(CLICK_WEBVIEW, 500);
}
return false;
}

@Override
public boolean handleMessage(Message msg) {
if (msg.what == CLICK_URL){
    Toast.makeText(this, "URL is clicked", Toast.LENGTH_SHORT).show();
    donext=true;
    return true;
}
if (msg.what == CLICK_WEBVIEW){
    if(donext==true)
    {
      //do nothing
    }else{
    Toast.makeText(this, "WebView is clicked", Toast.LENGTH_SHORT).show();
     }
    return true;
}
return false;
}
}

这篇关于Web视图仅在未单击链接的情况下触摸检测android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:09