简体   繁体   中英

How to implement multi button click event in Android

I have two seperate buttons. I want to do something only when they are BOTH clicked at same time. How can I implement that?

I'm new to Android, and I searched a lot, only finding how to set one listener for multi buttons.

Keep a boolean to track click of either button. While that flag is true, if another is clicked too, do your work. Disable the flag after some threshold time. Something like this:

  countDownTimer = new CountDownTimer(500,100) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            isOneBtnClicked = false;
        }
    };

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isOneBtnClicked) {
                countDownTimer.cancel();
                isOneBtnClicked = false;
                performSomething(); //whatever you wanted to do
            }
            else {
                isOneBtnClicked = true;
                countDownTimer.start();
            }
        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isOneBtnClicked) {
                countDownTimer.cancel();
                isOneBtnClicked = false;
                performSomething(); //whatever you wanted to do
            }
            else {
                isOneBtnClicked = true;
                countDownTimer.start();
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM