调用api
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
GestureDetector gestureDetector = new GestureDetector(Activity.this, new GestureDetector.SimpleOnGestureListener() {
/**
* 发生确定的单击时执行
* @param e
* @return
*/
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {//单击事件
Toast.makeText(Activity.this,"这是单击事件", Toast.LENGTH_SHORT).show();
return super.onSingleTapConfirmed(e);
}
/**
* 双击发生时的通知
* @param e
* @return
*/
@Override
public boolean onDoubleTap(MotionEvent e) {//双击事件
Toast.makeText(Activity.this,"这是单击事件",Toast.LENGTH_SHORT).show();
return super.onDoubleTap(e);
}
/**
* 双击手势过程中发生的事件,包括按下、移动和抬起事件
* @param e
* @return
*/
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return super.onDoubleTapEvent(e);
}
});
自定义:
public class MainActivity extends Activity {
long mLastTime=0;
long mCurTime=0;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
Toast.makeText(MainActivity.this,"这是单击事件",Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(MainActivity.this,"这是双击事件",Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLastTime=mCurTime;
mCurTime= System.currentTimeMillis();
if(mCurTime-mLastTime<300){//双击事件
mCurTime =0;
mLastTime = 0;
handler.removeMessages(1);
handler.sendEmptyMessage(2);
}else{//单击事件
handler.sendEmptyMessageDelayed(1, 310);
}
}
});
}
}