安卓singleTap /的OnClick中的WebView [英] Android singleTap/OnClick in WebView
问题描述
我加载Web视图以HTML字符串,我想显示导航按钮时,在网络上查看用户水龙头,我试着用onTouch监听器,触摸事件滚动和攻丝时提出,但 我要赶单一的水龙头/ clickEvent,不能滚动事件触动......,我实现了SetOnClickListener为的WebView和LinearLayout中,没有一个人不工作,我 任何有关此帮助
i am loading Web view with Html String,i want to show navigate buttons when the user tap on the web view,i tried With onTouch listener,touch event raises when scrolling and tapping but i want to catch single tap/clickEvent, Not scroll Event touches..., i implemented SetOnClickListener for both WebView and LinearLayout,None of them is not working for me Any help regarding this
推荐答案
的WebView不支持OnClickListener。而且它消耗的触摸事件,即使没有发生任何事情的网页这样的祖先的观点(如您的LinearLayout)上没有任何机会产生一个onclick事件。这是非常可惜的。
WebView does not support the OnClickListener. And also it does consume touch events even though nothing happened on the web page thus ancestors views (like your LinearLayout) don't have any chance to produce an OnClick event. It is very unfortunate.
作为一种解决办法我伸出一个RelativeLayout的,把我的WebView里面。在RelativeLayout的我已经覆盖onInterceptTouchEvent并寻找水龙头事件。如果检测到抽头则RelativeLayout的的OnClickListener被调用,performClick()。
As a workaround I extended a RelativeLayout and put my WebView inside it. In the RelativeLayout I've overwritten onInterceptTouchEvent and were looking for tap events. If a tap is detected then the RelativeLayout's OnClickListener is invoked with performClick().
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);
}
}
这篇关于安卓singleTap /的OnClick中的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!