即使用户没有从 Android 中的视图释放触摸,如何以编程方式取消 OnTouchListener 回调?
How to programmatically cancel the OnTouchListener callback even if the user haven't released the touch from a view in Android?
我正在开发一个录音应用程序。它的工作方式是,当用户按下并移动录制按钮时,按钮会随着手指移动。我已经创建了一个边界,当手指越过该边界时,我希望按钮执行 hide()
动画并返回到它的原始位置。
如果发生MotionEvent.ACTION_UP
或MotionEvent.ACTION_CANCEL
事件,整个过程工作正常,但即使触摸越过边界,hide()
操作也不会发生。按钮在边界外时有时会来回运动。即使我将视图的 visibility
设置为 false
,触摸事件仍会被调用。
我也在 logcat 中得到输出 (Log.e("MSG","boundary crossed");
)。
这是代码:
int recordButtonStartX;
microPhoneListner=new View.OnTouchListener() {
@Override
public boolean onTouch(View v, final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
recordButtonStartX = (int) event.getX();
this.floatingRecordButton.display(event.getX());
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
this.floatingRecordButton.hide(event.getX());
break;
case MotionEvent.ACTION_MOVE:
int tempX = (int) event.getX();
if ((recordButtonStartX - tempX) > 200) {
Log.e("MSG","boundary crossed");
this.floatingRecordButton.hide(event.getX());
}
else
{
this.floatingRecordButton.moveTo(event.getX());
}
break;
}
recordMsgButton.setOnTouchListener(microPhoneListner);
要为任何 View
释放 onTouchListener
,请将侦听器设置为 null
。
recordMsgButton.setOnTouchListener(null);
或
满足您的条件后,您可以将其他侦听器设置为 View
。
创建另一个听众
public final OnTouchListener mTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent rawEvent) {
return false;
}
};
当您想禁用侦听器时,请将其他侦听器设置为该视图
v.setOnTouchListener(mTouchListener);
我正在开发一个录音应用程序。它的工作方式是,当用户按下并移动录制按钮时,按钮会随着手指移动。我已经创建了一个边界,当手指越过该边界时,我希望按钮执行 hide()
动画并返回到它的原始位置。
如果发生MotionEvent.ACTION_UP
或MotionEvent.ACTION_CANCEL
事件,整个过程工作正常,但即使触摸越过边界,hide()
操作也不会发生。按钮在边界外时有时会来回运动。即使我将视图的 visibility
设置为 false
,触摸事件仍会被调用。
我也在 logcat 中得到输出 (Log.e("MSG","boundary crossed");
)。
这是代码:
int recordButtonStartX;
microPhoneListner=new View.OnTouchListener() {
@Override
public boolean onTouch(View v, final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
recordButtonStartX = (int) event.getX();
this.floatingRecordButton.display(event.getX());
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
this.floatingRecordButton.hide(event.getX());
break;
case MotionEvent.ACTION_MOVE:
int tempX = (int) event.getX();
if ((recordButtonStartX - tempX) > 200) {
Log.e("MSG","boundary crossed");
this.floatingRecordButton.hide(event.getX());
}
else
{
this.floatingRecordButton.moveTo(event.getX());
}
break;
}
recordMsgButton.setOnTouchListener(microPhoneListner);
要为任何 View
释放 onTouchListener
,请将侦听器设置为 null
。
recordMsgButton.setOnTouchListener(null);
或
满足您的条件后,您可以将其他侦听器设置为 View
。
创建另一个听众
public final OnTouchListener mTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent rawEvent) {
return false;
}
};
当您想禁用侦听器时,请将其他侦听器设置为该视图
v.setOnTouchListener(mTouchListener);