如何在Android中实现双击按钮? 我应该使用OnDoubleTapListener吗?
答案 0 :(得分:29)
int i = 0;
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
i++;
Handler handler = new Handler();
Runnable r = new Runnable() {
@Override
public void run() {
i = 0;
}
};
if (i == 1) {
//Single click
handler.postDelayed(r, 250);
} else if (i == 2) {
//Double click
i = 0;
ShowDailog();
}
}
});
答案 1 :(得分:4)
这可能是一个很好的起点:
Android: How to detect double-tap?
我建议切换到更原生的方式,例如长按(回答链接的问题)或更有创意的东西(使用多点触控),除非您偏向于Windows默认的双击处理方式?
你可能有正当的理由 - 双击 毕竟比长按更快。
答案 2 :(得分:3)
我写这个是为了在地图应用程序中双击弹出Toast消息:
private long lastTouchTime = -1;
@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
GeoPoint p = null;
if (e.getAction() == MotionEvent.ACTION_DOWN) {
long thisTime = System.currentTimeMillis();
if (thisTime - lastTouchTime < 250) {
// Double click
p = mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY());
lastTouchTime = -1;
} else {
// too slow
lastTouchTime = thisTime;
}
}
if (p != null) {
showClickedLocation(p);// Raise a Toast
}
return false;
}
答案 3 :(得分:3)
这是一个进行双击的好网站... 我用它并且工作了。
http://mobile.tutsplus.com/tutorials/android/android-gesture/
答案 4 :(得分:1)
private long lastTouchTime = 0;
private long currentTouchTime = 0;
...
@Override
public void onClick(View view) {
lastTouchTime = currentTouchTime;
currentTouchTime = System.currentTimeMillis();
if (currentTouchTime - lastTouchTime < 250) {
Log.d("Duble","Click");
lastTouchTime = 0;
currentTouchTime = 0;
}
}
答案 5 :(得分:0)
我用它并工作过:
public class DoubleClickTest extends Activity {
String TAG = "DoubleOrSingleClickTest";
private boolean waitDouble = true;
private static final int DOUBLE_CLICK_TIME = 350; // double click timer
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.double_click_test);
Button button = (Button) findViewById(R.id.buttonDoubleOrSingleClicked);
button.setOnClickListener(listenerDoubleOrSingle);
}
View.OnClickListener listenerDoubleOrSingle = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (waitDouble == true) {
waitDouble = false;
Thread thread = new Thread() {
@Override
public void run() {
try {
sleep(DOUBLE_CLICK_TIME);
if (waitDouble == false) {
waitDouble = true;
singleClick();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
} else {
waitDouble = true;
doubleClick();
}
}
};
// single event
private void singleClick() {
Log.i(TAG, "singleClick");
}
// double event
private void doubleClick() {
Log.i(TAG, "doubleClick");
}
}
答案 6 :(得分:0)
您可以通过继承View.OnClickListener
并添加监听器的回调来创建DoubleTapListener。
MyDoubleClickListener.class
public class MyDoubleClickListener implements View.OnClickListener{
private boolean isRunning= false;
private int resetInTime =500;
private int counter=0;
private DoubleClickCallback listener;
public DoubleTapListener(Context context)
{
listener = (DoubleClickCallback)context;
}
@Override
public void onClick(View v) {
if(isRunning)
{
if(counter==1) //<-- makes sure that the callback is triggered on double click
listener.onDoubleClick(v);
}
counter++;
if(!isRunning)
{
isRunning=true;
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(resetInTime);
isRunning = false;
counter=0;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
}
DoubleClickCallback.class
public interface DoubleClickCallback {
public void onDoubleClick(View v);
}
您已完成。您可以在任何活动中使用此监听器。
如何在我的活动中使用此DoubleClickListener?
在活动中实施回调并覆盖方法。
public class MainActivity extends AppCompatActivity implements MyDoubleClickListener{
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new DoubleTapListener(this)); //<-- Set listener
}
@Override
public void onDoubleClick(View v) {
// Toast to show double click
}
}
重要的一点是使用此概念,您可以创建任何类型的侦听器(三次单击侦听器)
查看完整的工作代码HERE