本文介绍了Android中长按向下和长按向上的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要两个单独的事件,分别用于长按向下和长按向上.如何在Android中做到这一点?

I want two separate event for long click Down and Long click up. How can I do this in 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;
    }
}

在一个活动中我这样称呼它

and in an activity I called it like this

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中获取任何日志消息谁能帮我;我哪里错了?

But still am not getting any of the Log messages in logcatCan anyone help me; where I am wrong ?

推荐答案

onKeyXXX()方法用于来自键盘或菜单键,搜索键等硬键的键事件.

onKeyXXX() methods are for Key Events from keyboard or hard keys such as menu key, search key, and so on.

如果要检测触摸事件(在Android中称为MotionEvent),则必须覆盖onTouchEvent(MotionEvent e)方法,并使用GestureDetector类来识别长按.

If you want to detect touch events, which is called MotionEvent in Android, you have to override onTouchEvent(MotionEvent e) method and use GestureDetector class for identifying long press.

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

这篇关于Android中长按向下和长按向上的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 18:47