我怎样才能获得的WebView onclick事件中的android?事件中、WebView、onclick、android

2023-09-12 00:11:21 作者:酒伴

我只是想知道对的WebView以外的其他超链接,当用户点击。我在那个点击我要显示/隐藏我的活动的观点,即持有的WebView。任何建议?

I just want to know the when user click on webview other then hyperlink. I On that click i want to show/hide a view of my activity that hold webview. Any suggestion?

推荐答案

我看了看这一点,我发现一个的WebView 似乎并没有发送单击事件到 OnClickListener 。如果有任何人能证明我错了,或告诉我,为什么那么我很想听到它。

I took a look at this and I found that a WebView doesn't seem to send click events to an OnClickListener. If anyone out there can prove me wrong or tell me why then I'd be interested to hear it.

我确实发现是,的WebView 将发送触摸事件的 OnTouchListener 。它有自己的的onTouchEvent 的方法,但我只似乎得到 MotionEvent.ACTION_MOVE 使用该方法。

What I did find is that a WebView will send touch events to an OnTouchListener. It does have its own onTouchEvent method but I only ever seemed to get MotionEvent.ACTION_MOVE using that method.

所以,既然我们可以得到一个注册触摸事件监听器事件,剩下唯一的问题是如何绕过任何行动要执行的触摸当用户点击一个网址。

So given that we can get events on a registered touch event listener, the only problem that remains is how to circumvent whatever action you want to perform for a touch when the user clicks a URL.

这可以通过一些花哨处理程序步法通过发送延迟的消息触摸,然后除去那些触摸消息如果触摸被点击的URL的用户造成的实现。

This can be achieved with some fancy Handler footwork by sending a delayed message for the touch and then removing those touch messages if the touch was caused by the user clicking a URL.

下面是一个例子:

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

private static final int CLICK_ON_WEBVIEW = 1;
private static final int CLICK_ON_URL = 2;

private final Handler handler = new Handler(this);

private WebView webView;
private WebViewClient client;

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

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

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

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

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

@Override
public boolean handleMessage(Message msg) {
    if (msg.what == CLICK_ON_URL){
        handler.removeMessages(CLICK_ON_WEBVIEW);
        return true;
    }
    if (msg.what == CLICK_ON_WEBVIEW){
        Toast.makeText(this, "WebView clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}
}

希望这有助于。

Hope this helps.

 
精彩推荐
图片推荐