在我的应用程序中,我有一个按钮。单击和双击按钮将执行不同的操作。我该怎么做呢?谢谢。
在我的应用程序中,我有一个按钮。单击和双击按钮将执行不同的操作。我该怎么做呢?谢谢。
很简单,只需要覆盖重写。
OnClickListener的onClick方法
public abstract class DoubleClickListener implements View.OnClickListener {
private static final long DEFAULT_QUALIFICATION_SPAN = 200;
private boolean isSingleEvent;
private long doubleClickQualificationSpanInMillis;
private long timestampLastClick;
private Handler handler;
private Runnable runnable;
public DoubleClickListener() {
doubleClickQualificationSpanInMillis = DEFAULT_QUALIFICATION_SPAN;
timestampLastClick = 0;
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
if (isSingleEvent) {
onSingleClick();
}
}
};
}
@Override
public void onClick(View v) {
if((SystemClock.elapsedRealtime() - timestampLastClick) < doubleClickQualificationSpanInMillis) {
isSingleEvent = false;
handler.removeCallbacks(runnable);
onDoubleClick();
return;
}
isSingleEvent = true;
handler.postDelayed(runnable, DEFAULT_QUALIFICATION_SPAN);
timestampLastClick = SystemClock.elapsedRealtime();
}
public abstract void onDoubleClick();
public abstract void onSingleClick();
}
使用方法
button.setOnClickListener(new DoubleClickListener() {
@Override
public void onDoubleClick() {
Log.i("onClick", "double");
}
@Override
public void onSingleClick() {
Log.i("onClick", "single");
}
});
private static final long DOUBLE_PRESS_INTERVAL = 250; // in millis
private long lastPressTime;
private boolean mHasDoubleClicked = false;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Get current time in nano seconds.
long pressTime = System.currentTimeMillis();
// If double click...
if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
Toast.makeText(getApplicationContext(), "Double Click Event", Toast.LENGTH_SHORT).show();
mHasDoubleClicked = true;
}
else { // If not double click....
mHasDoubleClicked = false;
Handler myHandler = new Handler() {
public void handleMessage(Message m) {
if (!mHasDoubleClicked) {
Toast.makeText(getApplicationContext(), "Single Click Event", Toast.LENGTH_SHORT).show();
}
}
};
Message m = new Message();
myHandler.sendMessageDelayed(m,DOUBLE_PRESS_INTERVAL);
}
// record the last time the menu button was pressed.
lastPressTime = pressTime;
return true;
}
您可能需要考虑不使用DoubleTap。这不是Android的正常行为。
当我第一次在Android上编程时,我经常遇到一些非常“困难”的事情。随着时间的推移,我发现其中许多都很困难,因为它们是一个相当糟糕的想法。
如果您正在移植iOS应用程序或模拟iOS应用程序的行为,则可能需要将UI转换为Android样式行为并使用longPress或其他“androidy”手势。
这里有一个类似的问题和答案:
您需要实现 GestureDetector 并将代码放在 单击/双击
中。
TestActivity.java
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//putyour first activity call.
}
}
iv.setOnTouchListener(new OnTouchListener() {
GestureDetector gestureDetector = new GestureDetector(context, new MyGestureDetector(context));
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
GestureDetector.java
类。public class MyGestureDetector extends SimpleOnGestureListener {
public Context context;
public String phno;
public MyGestureDetector(Context con) {
this.context=con;
}
@Override
public boolean onDown(MotionEvent e) {
return super.onDown(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
System.out.println("in Double tap");
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("in single tap up");
//put your second activity.
return super.onSingleTapUp(e);
}
}
感谢@NikolaDespotoski。
你可以从以下URL中查看DOUBLE-TAP example,它被用于listView。我希望它对你有用。
https://nodeload.github.com/NikolaDespotoski/DoubleTapListView/zip/master
虽然现在有点晚了,但是任何人看到这一点都可以理解。
int number_of_clicks = 0;
boolean thread_started = false;
final int DELAY_BETWEEN_CLICKS_IN_MILLISECONDS = 250;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
++number_of_clicks;
if(!thread_started){
new Thread(new Runnable() {
@Override
public void run() {
thread_started = true;
try {
Thread.sleep(DELAY_BETWEEN_CLICKS_IN_MILLISECONDS);
if(number_of_clicks == 1){
client.send(AppHelper.FORMAT_LEFT_CLICK);
} else if(number_of_clicks == 2){
client.send(AppHelper.FORMAT_DOUBLE_CLICK);
}
number_of_clicks = 0;
thread_started = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
});
解释:
abstract class DoubleClickListener : View.OnClickListener {
private val DEFAULT_QUALIFICATION_SPAN = 200L
private var isSingleEvent = false
private val doubleClickQualificationSpanInMillis =
DEFAULT_QUALIFICATION_SPAN
private var timestampLastClick = 0L
private val handler = Handler(Looper.getMainLooper())
private val runnable: () -> Unit = {
if (isSingleEvent) {
onSingleClick()
}
}
override fun onClick(v: View) {
if (SystemClock.elapsedRealtime() - timestampLastClick < doubleClickQualificationSpanInMillis) {
isSingleEvent = false
handler.removeCallbacks(runnable)
onDoubleClick()
return
}
isSingleEvent = true
handler.postDelayed(runnable, DEFAULT_QUALIFICATION_SPAN)
timestampLastClick = SystemClock.elapsedRealtime()
}
abstract fun onDoubleClick()
abstract fun onSingleClick()
}
import android.os.SystemClock;
import android.view.View;
/*****
* Implement to fix the double click problem.
* Avoid the fast double click for button and images.
*/
public abstract class OnSingleClickListener implements View.OnClickListener {
private long prevClickTime =0;
@Override
public void onClick(View v) {
_onClick(v);
}
private synchronized void _onClick(View v){
long current = SystemClock.elapsedRealtime();
if(current-prevClickTime> getGap()){
onSingleClick(v);
prevClickTime = SystemClock.elapsedRealtime();
}else {
onDoubleClick(v);
prevClickTime = 0;
}
}
public abstract void onSingleClick(View v);
public abstract void onDoubleClick(View v);
/********
*
* @return The time in ms between two clicks.
*/
public long getGap(){
return 500L; //500ms
}
}
安卓系统中的双击和单击
适用于几乎所有版本的解决方案
检测在您想要定义的标准时间内的事件类型。它可以加速地、连续地检测事件,例如在一定时间内通过视频。
//general definition
private CountDownTimer countDownTimer;
private int click_duble = 1;
button.setOnClickListener(view -> {
if (countDownTimer == null) {
float Second= (float) 0.25; //Detecting the type of event within a quarter of a second
countDownTimer= new CountDownTimer((long) (Second * 1000), 50) {
@Override public void onTick(long l){}
@Override
public void onFinish() {
if (click_duble >= 2) {
Toast.makeText(player_fb.this, "Double", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(player_fb.this, "Single", Toast.LENGTH_SHORT).show();
}
click_duble = 1;
countDownTimer = null;
}};countDownTimer.start();
}else
click_duble += 1;
});
对于多次点击,之前的解决方案无法工作,我测试过但失败了。
因此,我建议使用RxBinding和ProgressDialog。
当点击按钮时,ProgressDialog会显示设置,它不能被取消,修复它。