长期活动单击向下长按了Android中(Events for Long click down and

2019-07-19 21:05发布

我想长期两个独立的事件单击向下并长按了。 我如何能在Android中做到这一点?

我曾尝试如下:

public class FfwRewButton extends ImageButton {

    public interface ButtonListener {

        void OnLongClickDown(View v);

        void OnLongClickUp(View v);
    }

    private ButtonListener mListener;

    private boolean mLongClicked = false;

    public FfwRewButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFocusable(true);
        setLongClickable(true);
    }

    public FfwRewButton(Context context) {
        this(context, null);
    }

    public FfwRewButton(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.imageButtonStyle);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        Log.d("my listener", "long press");
        mLongClicked = true;
        mListener.OnLongClickDown(this);
        return super.onKeyLongPress(keyCode, event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d("my listener", "key down");
        mLongClicked = false;
        if (true) {
            super.onKeyDown(keyCode, event);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Log.d("my listener", "key up");
        if (mLongClicked)
            mListener.OnLongClickUp(this);
        return super.onKeyUp(keyCode, event);
    }

    public void setFfwRewButtonListener(ButtonListener listener) {
        mListener = listener;
    }
}

并在活动我把它叫做这样

private FfwRewButton.ButtonListener mListener = new FfwRewButton.ButtonListener() {

        @Override
        public void OnLongClickUp(View v) {
            Log.d(TAG, "longClickup");
        }

        @Override
        public void OnLongClickDown(View v) {
            Log.d(TAG, "longClickdown");
        }
    };

但我仍然没有得到任何的logcat谁能帮我的日志信息的; 我在哪里错了?

Answer 1:

onKeyXXX()方法是从键盘或硬按键,如菜单键,搜索键,等关键事件。

如果你想检测触摸事件,这就是所谓MotionEvent在Android中,你必须覆盖onTouchEvent(MotionEvent e)方法,并使用GestureDetector类识别长按。

private GestureDetector mGestureDetector;

public FfwRewButton(...) {
    //....
    mGestureDetector = new GestureDetector(context, 
        new GestureDetector.SimpleOnGestureListener() {
            public boolean onDown(MotionEvent e) {
                mLongClicked = false;
                return true;
            }
            public void onLongPress(MotionEvent e) {
                mLongClicked = true;
                // long press down detected
            }
        });
    }

    public boolean onTouchEvent(MotionEvent e) {
        mGestureDetector.onTouchEvent(e);
        if (mLongClicked && e.getAction() == ACTION_UP) {
           // long press up detected
        }
    }
}


Answer 2:

像这样的东西将让你在正确的道路上,

我没有编译,所以你可能需要纠正一些语法的东西,但你的目标可以用这个概念来实现

OnTouchListener mTouchListener = new OnTouchListener(){
  private totalTimeDown = -1;
  private downTime = -1;
  public boolean onTouch(View v, MotionEvent me){
    if(me.getAction() == MotionEvent.ACTION_DOWN){
        downTime = System.getCurrentTimeInMillis();
        return true;
    }

    if(me.getAction() == MotionEvent.ACTION_UP){
        totalTimeDown = System.getCurrentTimeInMillis() - downTime;
        if(totalTimeDown > 500){
            //Finger was down long enough for "longClick"
            return true;
        }
    }
    return false;
  }
});


文章来源: Events for Long click down and long click up in Android