我发现这段代码显示了如何创建一个 GestureListener 来监听双击,但我一直无法弄清楚如何将其应用到我的 EditText,以便只有在用户双击 EditText。
任何帮助将不胜感激。
public class MyView extends View {
GestureDetector gestureDetector;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// creating new gesture detector
gestureDetector = new GestureDetector(context, new GestureListener());
}
// skipping measure calculation and drawing
// delegate the event to the gesture detector
@Override
public boolean onTouchEvent(MotionEvent e) {
return gestureDetector.onTouchEvent(e);
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
// event when double tap occurs
@Override
public boolean onDoubleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");
return true;
}
}
}
最佳答案
我自己用 onTouchListener 实现双击逻辑:
EditText edit = (EditText) findViewById(R.id.edit);
edit.setOnTouchListener(new OnTouchListener() {
long oldTime = 0;
@Override
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(System.currentTimeMillis()-oldTime<300){
Log.i("TAG", "Double Click");
}
oldTime=System.currentTimeMillis();
}
return true;
}
});
关于java - 如何将手势监听器应用于 EditText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17307045/