我想在双击TextView时触发某些事件。
final GestureDetector gestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener() {
public boolean onDoubleTap(MotionEvent e) {
Log.e("", "Open new activty here");
return true;
}
});
TextView tv = (TextView) findViewById(R.id.editTextID);
tv.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
返回ontapevent
答案 0 :(得分:2)
您可能需要创建新的类实现Onclick。正常点击将延迟400毫秒,直到使用Timer检测到第二次点击。如果不存在400毫秒后没有第二次点击,则会考虑单击
public abstract class DoubleClickListener implements OnClickListener {
private Timer timer = null; //at class level;
private int DELAY = 400;
private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
long lastClickTime = 0;
@Override
public void onClick(View v) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
processDoubleClickEvent(v);
} else {
processSingleClickEvent(v);
}
lastClickTime = clickTime;
}
public void processSingleClickEvent(final View v){
final Handler handler=new Handler();
final Runnable mRunnable=new Runnable(){
public void run(){
onSingleClick(v); //Do what ever u want on single click
}
};
TimerTask timertask=new TimerTask(){
@Override
public void run(){
handler.post(mRunnable);
}
};
timer=new Timer();
timer.schedule(timertask,DELAY);
}
public void processDoubleClickEvent(View v){
if(timer!=null)
{
timer.cancel(); //Cancels Running Tasks or Waiting Tasks.
timer.purge(); //Frees Memory by erasing cancelled Tasks.
}
onDoubleClick(v);//Do what ever u want on Double Click
}
public abstract void onSingleClick(View v);
public abstract void onDoubleClick(View v);
}
如何使用
view.setOnClickListener(new DoubleClickListener() {
@Override
public void onSingleClick(View v) {
}
@Override
public void onDoubleClick(View v) {
// Your code here
}
});
答案 1 :(得分:0)
您需要实现所有这一切,请使用此代码:
final GestureDetector gd = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
//here is the method for double tap
@Override
public boolean onDoubleTap(MotionEvent e) {
//your action here for double tap e.g.
//Log.d("OnDoubleTapListener", "onDoubleTap");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return true;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
});
//here yourView is the View on which you want to set the double tap action
yourView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gd.onTouchEvent(event);
}
});
已经说过在android中使用longclick比较常见,
希望有所帮助
答案 2 :(得分:0)
其中一个懒惰的实现
private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
long lastClickTime = 0;
TextView tv = (TextView) findViewById(R.id.editTextID);
tv.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
lastClickTime = 0;
//double click
} else {
//single click
}
lastClickTime = clickTime;
}
});
但如果反复使用双击,不要懒惰。走得更好.. 创建一个抽象类n继续..
public abstract class DoubleClickListener implements OnClickListener {
private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
long lastClickTime = 0;
@Override
public void onClick(View v) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
onDoubleClick(v);
lastClickTime = 0;
} else {
onSingleClick(v);
}
lastClickTime = clickTime;
}
public abstract void onSingleClick(View v);
public abstract void onDoubleClick(View v);
}
在任何地方使用
view.setOnClickListener(new DoubleClickListener() {
@Override
public void onSingleClick(View v) {
}
@Override
public void onDoubleClick(View v) {
}
});
快乐编码