Single click and double click of a button in android

前端 未结 9 1295
遇见更好的自我
遇见更好的自我 2020-12-05 22:09

In my application i have a button. After single and double clicking of the button will perform separate operation. How can i do that? Thanks

相关标签:
9条回答
  • 2020-12-05 22:31

    The up solution cannot work for multi click, i test it but failed.

    So i suggest to use RxBinding with ProgressDialog.

    when click button, the progressDialog show setting it cannot be cancel, Fix it.

    0 讨论(0)
  • 2020-12-05 22:32

    You may want to consider not using a DoubleTap. It is not a normal Android behavior.

    When I first started programming on the Android, I kept running into things that were really "hard" to do on the android. Over time, I've found that many of them were difficult because they were a pretty bad idea.

    If you are porting an iOS app, or emulating an iOS app's behavior, you may want to consider converting the UI over to Android style behaviors and use a longPress or other 'androidy' gestures.

    Here is a similar question and answer:

    Android: How to detect double-tap?

    0 讨论(0)
  • 2020-12-05 22:33

    Well it is simple just override.

    onClick method of OnClickListener

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

    Usage

     button.setOnClickListener(new DoubleClickListener() {
            @Override
            public void onDoubleClick() {
                Log.i("onClick", "double");
            }
    
            @Override
            public void onSingleClick() {
                Log.i("onClick", "single");
            }
        });
    
    0 讨论(0)
提交回复
热议问题