3

我想要两个单独的事件来长按向下和长按向上。我怎样才能在 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 中收到任何日志消息有人可以帮助我吗?我错在哪里?

4

3 回答 3

7

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
        }
    }
}
于 2013-02-14T14:46:46.183 回答
3

这样的事情会让你走上正确的道路,

我没有编译,所以您可能需要更正一些语法问题,但您的目标可以通过这个概念来实现

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;
  }
});
于 2013-02-14T14:41:49.010 回答
0
holder.layout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                holder.layoutItem.setVisibility(View.GONE);
                                holder.layoutDelete.setVisibility(View.VISIBLE);
                            }
                        }, 1000);
                        // PRESSED
                        return true; // if you want to handle the touch event
                    case MotionEvent.ACTION_UP:
                        holder.layoutItem.setVisibility(View.VISIBLE);
                        holder.layoutDelete.setVisibility(View.GONE);
                        // RELEASED
                        return true; // if you want to handle the touch event
                }
                return false;
            }
        });
于 2019-09-21T18:42:22.917 回答