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