gpt4 book ai didi

android - 为什么 android onLongPress 总是在 onDoubleTap 之后触发?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:46:50 24 4
gpt4 key购买 nike

我根据以下代码在按钮上放置了 onLongPress 和 onDoubleTap 操作:

...
GestureDetector detector = new GestureDetector(this, new TapDetector());

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this, new TapDetector());
...
}

private class TapDetector extends GestureDetector.SimpleOnGestureListener {

@Override
public boolean onDoubleTap(MotionEvent e) {
// Do something
return true;
}

@Override
public void onLongPress(MotionEvent e) {
// Do something
}
}

Button incomeButton = (Button) findViewById(R.id.income);
button.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});

我总是看到 onDoubleClick 触发并执行后触发 onLongPress。这种违反直觉的行为的原因是什么以及如何避免?

已更新我已将源代码更改为更具体

public class MainActivity extends Activity { 
private GestureDetector detector;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this, new TapDetector());

Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("************* onTouch *************");
detector.onTouchEvent(event);
return true;
}
});
}

class TapDetector extends GestureDetector.SimpleOnGestureListener {

@Override
public void onLongPress(MotionEvent e) {
System.out.println("************* onLongPress *************");
}

@Override
public boolean onDoubleTap(MotionEvent e) {
System.out.println("************* onDoubleTap *************");
Intent intent = new Intent();
intent.setClass(getApplicationContext(), NewActivity.class);
intent.putExtra("parameterName", "parameter");
startActivity(intent);
return true;
}
}
}

这是我点击 onDoubleTap 后的日志。你可以在最后看到 onLongPress - 我从不点击它。

I/System.out( 1106): ************* onTouch *************
I/System.out( 1106): ************* onTouch *************
I/System.out( 1106): ************* onTouch *************
I/System.out( 1106): ************* onDoubleTap *************
I/ActivityManager( 59): Starting activity: Intent { cmp=my.tapdetector/.NewActivity (has extras) }
I/ActivityManager( 59): Displayed activity my.tapdetector/.NewActivity: 324 ms (total 324 ms)
I/System.out( 1106): ************* onLongPress *************

更新 我找到了解决方案。为避免 onLongPress 触发两次,需要进行以下更改:

首先:detector.setIsLongpressEnabled(true);在 onTouch(View v, MotionEvent 事件)

    button.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("************* onTouch *************");
detector.onTouchEvent(event);
detector.setIsLongpressEnabled(true);
return true;
}
});

其次:添加 detector.setIsLongpressEnabled(false);在 onDoubleTap(MotionEvent e)

     public boolean onDoubleTap(MotionEvent e) {
detector.setIsLongpressEnabled(false);
System.out.println("************* onDoubleTap *************");
Intent intent = new Intent();
intent.setClass(getApplicationContext(), NewActivity.class);
intent.putExtra("parameterName", "parameter");
startActivity(intent);
return true;
}

最佳答案

从技术上讲,这不应该发生

case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
mCurrentDownEvent = MotionEvent.obtain(ev);
mAlwaysInTapRegion = true;
mInLongPress = false;

if (mIsLongpressEnabled) {
mHandler.removeMessages(LONG_PRESS);
mHandler.sendEmptyMessageAtTime(LONG_PRESS, mCurrentDownEvent.getDownTime()
+ tapTime + longpressTime);
}
mHandler.sendEmptyMessageAtTime(SHOW_PRESS, mCurrentDownEvent.getDownTime() + tapTime);

因为在 ACTION_DOWN 或 ACTION_UP 事件上,所有 LONG_PRESS 消息事件都从队列中删除。因此,在第二次点击时,以下代码将删除长按事件。

 mHandler.removeMessages(LONG_PRESS);

Ninja edit : hacky workaround for your problem

     @Override
public void onLongPress(MotionEvent e) {
if(MainActivity.this.hasWindowFocus())
{
Log.d("Touchy", "Long tap");
}
}

关于android - 为什么 android onLongPress 总是在 onDoubleTap 之后触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7606921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com