0

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

3 回答 3

2

我会自己用 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;
        }
    });
于 2013-06-25T20:56:35.660 回答
0

史蒂文是对的,您可以在 onTouchListener 中使用计数器变量,当计数达到 2 时,在 editText 上调用 requestfocus() 。执行 MotionEvent.Action_Down 案例的逻辑。

于 2013-06-25T21:25:10.137 回答
0

这不是问题的答案,而是我用来检测 EditText 上的双击的替代方法。


这是我目前正在使用的代码。
您不一定必须使用所有这些代码。我包含了一些额外的代码,只是为了帮助您确定应该放置双击检测代码的位置。

import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity 
{

public long startTime1 = System.currentTimeMillis();
public long elapsedTime1 = 0;


@Override
   protected void onCreate(Bundle savedInstanceState) 
   {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   final  EditText myEditText = (EditText) findViewById(R.id.myEditText);

   myEditText.setOnTouchListener(new myDoubleClickDetector());
   }


   public class myDoubleClickDetector implements OnTouchListener 
   {
      public boolean onTouch(View v, MotionEvent event) 
      {
         if (event.getAction() == MotionEvent.ACTION_DOWN) 
         {
         elapsedTime1 = System.currentTimeMillis() - startTime1;
            if (elapsedTime1 > 500) //500 is the Windows standard double-click delay .. it works well
            {
            startTime1 = System.currentTimeMillis();
            return false;
            }
            else
            {
               if (elapsedTime1 > 50) //this 50 is just to help prevent errors .. you can change to 0 if you want
               {
               Toast.makeText(getApplicationContext(), "You just double-tapped", Toast.LENGTH_SHORT).show();
               startTime1 = System.currentTimeMillis();
               return true;
               }
            }
         }
      return false;   
      }
   }

}


以下是最重要的代码行:

public long startTime1 = System.currentTimeMillis();
public long elapsedTime1 = 0;

myEditText.setOnTouchListener(new myDoubleClickDetector());

   public class myDoubleClickDetector implements OnTouchListener 
   {
      public boolean onTouch(View v, MotionEvent event) 
      {
         if (event.getAction() == MotionEvent.ACTION_DOWN) 
         {
         elapsedTime1 = System.currentTimeMillis() - startTime1;
            if (elapsedTime1 > 500) //500 is the Windows standard double-click delay .. it works well
            {
            startTime1 = System.currentTimeMillis();
            return false;
            }
            else
            {
               if (elapsedTime1 > 50) //this 50 is just to help prevent errors .. you can change to 0 if you want
               {
               Toast.makeText(getApplicationContext(), "You just double-tapped", Toast.LENGTH_SHORT).show();
               startTime1 = System.currentTimeMillis();
               return true;
               }
            }
         }
      return false;   
      }
   }
于 2013-06-26T00:28:59.887 回答