我正在使用Html字符串加载Web视图,我想在用户点击Web视图时显示导航按钮,我尝试使用onTouch监听器,在滚动和点击时触摸事件引发但是 我想抓住单击/点击事件,不滚动事件触摸...,我为WebView和LinearLayout实现了SetOnClickListener,它们都不适用于我 有关此的任何帮助
答案 0 :(得分:24)
WebView
不支持OnClickListener
。即使网页上没有任何内容,它也会消耗触摸事件,因此祖先视图(如您的LinearLayout
)没有任何机会产生OnClick事件。非常不幸。
作为一种解决方法,我扩展了RelativeLayout
并将WebView
放在其中。在RelativeLayout
我已覆盖onInterceptTouchEvent
并正在寻找点击事件。如果检测到点按,则会使用RelativeLayout
调用OnClickListener
的{{1}}。
performClick()
}
答案 1 :(得分:3)
我修改了Zsolt Safrany的答案,并将他的onInterceptTouchEvent
内容放入webview的onTouch
方法中,并且它有效。
webview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG,"Got a touch event in the web view!");
final int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mMoveOccured = false;
mDownPosX = ev.getX();
mDownPosY = ev.getY();
break;
case MotionEvent.ACTION_UP:
if (!mMoveOccured) {
//click operation is here
}
break;
case MotionEvent.ACTION_MOVE:
if (Math.abs(ev.getX() - mDownPosX) > MOVE_THRESHOLD_DP || Math.abs(ev.getY() - mDownPosY) > MOVE_THRESHOLD_DP) {
mMoveOccured = true;
}
break;
}
return false;
} });
答案 2 :(得分:2)
从此页面:http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/
似乎必须正确捕获触摸事件。看看这个:
@Override
public boolean dispatchTouchEvent(MotionEvent e){
super.dispatchTouchEvent(e);
return mGestureDetector.onTouchEvent(e);
}
答案 3 :(得分:0)
工作代码::
final Boolean[] mMoveOccured = new Boolean[1];
final float[] mDownPosX = new float[1];
final float[] mDownPosY = new float[1];
final float MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density;
userPic.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mMoveOccured[0] = false;
mDownPosX[0] = event.getX();
mDownPosY[0] = event.getY();
break;
case MotionEvent.ACTION_UP:
if (!mMoveOccured[0]) {
Toast.makeText(v.getContext(), "Webview pressed", Toast.LENGTH_SHORT).show();
}
break;
case MotionEvent.ACTION_MOVE:
if (Math.abs(event.getX() - mDownPosX[0]) > MOVE_THRESHOLD_DP || Math.abs(event.getY() - mDownPosY[0]) > MOVE_THRESHOLD_DP) {
mMoveOccured[0] = true;
}
break;
}
return false;
}
});