WebView中的Android singleTap / OnClick

我正在使用Html字符串加载Web视图,我想在用户点击Web视图时显示导航按钮,我尝试使用onTouch监听器,在滚动和点击时触摸事件引发但我想捕获单击/ clickEvent,不滚动事件触摸…,我为WebView和LinearLayout实现了SetOnClickListener,它们都不适用于我任何关于此的帮助

WebView不支持OnClickListener 。 而且它确实消耗了触摸事件,即使网页上没有任何反应,因此祖先视图(如您的LinearLayout )没有任何机会产生OnClick事件。 非常不幸。

作为一种解决方法,我扩展了RelativeLayout并将我的WebView放在其中。 在RelativeLayout我覆盖了onInterceptTouchEvent并且正在寻找tap事件。 如果检测到tap,则使用performClick()调用RelativeLayoutOnClickListener

 public class TapAwareRelativeLayout extends RelativeLayout { private final float MOVE_THRESHOLD_DP = 20 * getResources().getDisplayMetrics().density; private boolean mMoveOccured; private float mDownPosX; private float mDownPosY; public TapAwareRelativeLayout(Context context) { super(context); } public TapAwareRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { 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) { // TAP occured performClick(); } 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 super.onInterceptTouchEvent(ev); } 

}

从这个页面: 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); } 

我修改了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; } }); 

工作代码::

 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; } });