調(diào)用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() {
/**
* 發(fā)生確定的單擊時執(zhí)行
* @param e
* @return
*/
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {//單擊事件
Toast.makeText(Activity.this,"這是單擊事件", Toast.LENGTH_SHORT).show();
return super.onSingleTapConfirmed(e);
}
/**
* 雙擊發(fā)生時的通知
* @param e
* @return
*/
@Override
public boolean onDoubleTap(MotionEvent e) {//雙擊事件
Toast.makeText(Activity.this,"這是單擊事件",Toast.LENGTH_SHORT).show();
return super.onDoubleTap(e);
}
/**
* 雙擊手勢過程中發(fā)生的事件鸥诽,包括按下静陈、移動和抬起事件
* @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);
}
}
});
}
}