android button long press,为什么android onLongPress总是在onDoubleTap之后被触发?

根据这段代码,我有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;

}

});

我经常看到onLongPress在onDoubleClick被触发并被执行后被触发.这种违反直觉行为的原因是什么,如何避免?

更新

我更改了我的源代码更具体

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发射两个变化需要做:

第一:detect.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;

}

});

第二:添加detect.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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值