7

在我的应用程序中,我有一个按钮。单击按钮和双击按钮后将执行单独的操作。我怎样才能做到这一点?谢谢

4

10 回答 10

9

您可能需要创建一个延迟变量,以区分单击和双击。

看到这段代码,

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;
    }
于 2013-03-12T04:46:20.960 回答
8

那么它很简单只是覆盖。

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");
        }
    });
于 2019-10-24T18:48:10.430 回答
6

您可能要考虑不使用 DoubleTap。这不是正常的 Android 行为。

当我第一次开始在 Android 上编程时,我不断遇到在 android 上真正“难以”做的事情。随着时间的流逝,我发现其中许多都很难,因为它们是一个非常糟糕的主意。

如果您要移植 iOS 应用程序或模拟 iOS 应用程序的行为,您可能需要考虑将 UI 转换为 Android 样式的行为并使用 longPress 或其他“androidy”手势。

这是一个类似的问题和答案:

Android:如何检测双击?

于 2013-03-12T04:49:10.293 回答
4

您必须实现GestureDetector并将您的代码放入single/double click.

测试活动.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);
    }   
}
于 2013-03-12T04:47:09.027 回答
3

感谢@NikolaDespotoski。_

您可以从以下URL检查DOUBLE-TAP 示例。在 listView 中使用。我希望它对你有用。

https://nodeload.github.com/NikolaDespotoski/DoubleTapListView/zip/master

于 2013-03-12T10:30:27.700 回答
3

虽然为时已晚,但任何人都可以弄清楚他们是否看到了这一点。

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();
        }
    }
});

解释:


在单击按钮之前,number_of_click 被初始化为 0。thread_started 是一个标志,用于检测线程之前是否启动过。现在,在单击按钮时,通过增量运算符增加按钮单击的次数。检查线程之前是否启动过,如果没有,则启动线程。在线程上,使用 number_of_clicks 应用您的逻辑。并且线程将等待下一毫秒,然后将通过您的逻辑。因此,现在您可以根据需要应用任意数量的点击。

于 2015-09-12T08:54:37.433 回答
1

通过继承View.OnClickListener并检查点击时间来区分单击或双击来解决这个问题,这也解决了快速点击的问题。此解决方案将带来较小的代码更改,只需替换 View.OnClickLister。您还可以覆盖 getGap() 以重新定义两次单击之间的时间。

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
    }
}
于 2016-03-03T08:51:10.313 回答
1

这是@saksham 在 Kotlin 中的回答。

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()
}
于 2021-07-12T22:13:43.790 回答
0

up 解决方案不能用于多次单击,我对其进行了测试但失败了。

所以我建议将 RxBinding 与 ProgressDialog 一起使用。

单击按钮时,progressDialog显示设置无法取消,修复它。

于 2017-04-06T03:10:56.110 回答
-2

我也遇到过同样的问题

setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                if(isFmOn()){
                   //stopFM
                }else{
                  //do other things
                }
            }
}

当我单击按钮时,FM 停止;但是当我双击时,FM 并没有停止。问题是单击按钮和双击按钮时,isFmOn() 的值不同。我用这个解决了这个问题:

setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                Thread.sleep(500);//500ms was enough to finish stopFM before the second click
                if(isFmOn()){
                   //stopFM
                }else{
                  //do other things
                }
            }
}
于 2013-05-15T08:07:28.713 回答