isa*_*ent 12 android double-click
我根据此代码将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;
}
});
Run Code Online (Sandbox Code Playgroud)
在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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我点击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 *************
Run Code Online (Sandbox Code Playgroud)
更新我找到了解决方案.为避免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;
}
});
Run Code Online (Sandbox Code Playgroud)
第二:添加detector.setIsLongpressEnabled(false); in 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;
}
Run Code Online (Sandbox Code Playgroud)
从技术上讲,这不应该发生
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);
Run Code Online (Sandbox Code Playgroud)
因为在 ACTION_DOWN 或 ACTION_UP 事件中,所有 LONG_PRESS 消息事件都会从队列中删除。因此,在第二次点击时,以下代码将删除长按事件。
mHandler.removeMessages(LONG_PRESS);
Run Code Online (Sandbox Code Playgroud)
忍者编辑:解决您问题的黑客方法
@Override
public void onLongPress(MotionEvent e) {
if(MainActivity.this.hasWindowFocus())
{
Log.d("Touchy", "Long tap");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6599 次 |
| 最近记录: |