Android:在屏幕上点击禁用按钮

时间:2015-11-17 08:45:03

标签: android android-viewpager listener gesture

当用户在屏幕上单击时,我想禁用我显示的按钮。我目前在MainActivity上有一个ViewPager和提到的按钮。

我必须使用哪个监听器来获取单击事件以及我必须在哪里“放”它,所以它不会覆盖我的viewPager的滑动手势? (我试图在我的viewPager上放一个onTouchListener,它确实有效,但我显然无法刷卡了)

我的插图代码(请保持温和,我是Android新手:)):

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener{ 
(some Code, onCreate etc...)
@Override
public boolean onTouchEvent(MotionEvent event) {
        int action = MotionEventCompat.getActionMasked(event);

        switch(action) {
            case (MotionEvent.ACTION_DOWN) :
                Log.d(DEBUG_TAG, "Action was DOWN");
                return true;
            case (MotionEvent.ACTION_MOVE) :
                Log.d(DEBUG_TAG,"Action was MOVE");
                return true;
            case (MotionEvent.ACTION_UP) :
                triggerButtons();
                Log.d(DEBUG_TAG,"Action was UP");
                return true;
            case (MotionEvent.ACTION_CANCEL) :
                Log.d(DEBUG_TAG,"Action was CANCEL");
                return true;
            case (MotionEvent.ACTION_OUTSIDE) :
                Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                        "of current screen element");
                return true;
            default :
                return super.onTouchEvent(event);
        }
}

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    triggerButtons();
    return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    return false;
}

正如您所看到的,当触摸/敲击发生后用户抬起手指时,我希望触发“triggerButtons”。我需要在onCreate中分配什么样的监听器来使其工作?

(如果你也知道如何听取doubleTap,那就太棒了!)

编辑:

我添加了

View.OnTouchListener mOnTouchListener; 
GestureDetector mGestureDetector; 

我在MainActivity中加入了onCreate:

mOnTouchListener = new View.OnTouchListener() { 
    @Override public boolean onTouch(View v, MotionEvent event) {  
    mGestureDetector.onTouchEvent(event); 
    return false; 
   } 
}; 

mViewPager.setOnTouchListener(mOnTouchListener); 

解决方案:

on onCreate:

mOnTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            touchEventDecider(event);
            return false;
        }
    };

方法touchEventDecider:

private boolean touchEventDecider(MotionEvent event) {
    int action = MotionEventCompat.getActionMasked(event);

    switch(action) {
        case (MotionEvent.ACTION_DOWN) :
            Log.d(DEBUG_TAG, "Action was DOWN");
            return true;
        case (MotionEvent.ACTION_MOVE) :
            Log.d(DEBUG_TAG,"Action was MOVE");
            return true;
        case (MotionEvent.ACTION_UP) :
            triggerButtons();
            Log.d(DEBUG_TAG,"Action was UP");
            return true;
        case (MotionEvent.ACTION_CANCEL) :
            Log.d(DEBUG_TAG,"Action was CANCEL");
            return true;
        case (MotionEvent.ACTION_OUTSIDE) :
            Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                    "of current screen element");
            return true;
        default :
            return super.onTouchEvent(event);
    }
}

1 个答案:

答案 0 :(得分:2)

OnTouchListener添加到ViewPager。在onTouchEvent方法的TouchListener方法中,将每个事件传递给GestureDetector并返回false。在GestureDetectortrue返回onDown,只留下onSingleTapUp

您的问题是,您通过返回onTouchEvent消费了true方法中的每一个触摸事件,因此事件不会发送到您的ViewPager,使其无法滚动。

编辑:如果您想在不使用GestureDetector的情况下自行识别单击,那么只需将OnTouchListener附加到您的ViewPager,但不要忘记在false方法中返回onTouchEvent