我有onLongPress並根據此代碼放在按鈕onDoubleTap操作:爲什麼android 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 *************
UPDATE我已經找到了解決辦法。爲了避免onLongPress發射變化需要做的事情:
第一:detector.setIsLongpressEnabled(真);在onTouch(視圖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(假);在onDoubleTap(MotionEvent五)
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;
}
不能重複這個,對不起。在這兩個事件上使用Log.i()時,它們會被正確調用。 –
體面的工作,看起來像一個內置的競爭條件,如接受答案的評論中所述。謝謝。 –