这不是问题的答案,而是我用来检测 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;
}
}