java - 如何将手势监听器应用于 EditText?

标签 java android view gesture double-click

我发现这段代码显示了如何创建一个 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/

相关文章:

Java RegEx 查找方法名称

java - 在android中有条件地显示所有联系人的最快方法

android - 在没有 Android Beam 的情况下使用 NFC 与两部 Android 手机通信

swift - 从 UITableViewCell 发送数据到 Controller MVC-Principle

java - 如何在 Java 中实现集合数据结构?

java - com.example.controller.UserController 中的字段 userRepository 需要类型为 'com.example.repository.UserRepository' 的 bean,但无法找到

java - 在java中填充数组

android - 用于集成测试的 Dagger Inject

wpf - 无法将 VM 中的类绑定(bind)到 WPF 中的 View

android - XML Inflater 没有看到任何 View ?