简体   繁体   中英

Android Development: How to set a timer to multiple checkboxes when they're unchecked?

How do I set a timer on all of the checkboxes at the same time if I were to uncheck them all at once? If I uncheck "checkbox1" and set up a timer for that checkbox, wouldn't unchecking "checkbox2" override that timer?

  • I have 3 checkboxes and 1 textview,
  • All checkboxes in the state of checked.
  • I want to create a timer when any of them is unchecked.
  • I wish to have a independent timer for each checkbox

For example, when "checkbox1" is unchecked, I wish to create a timer that lasts for 5 minutes and when it only has 1 minute left, I want it to change the textview's text to "you have 1 minute left" as a reminder that the timer is ending soon.

public void onCheckedChanged(final CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    if (!buttonView.isChecked()) {
        new CountDownTimer(300000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                if (millisUntilFinished < 60000) {
                    map.get(buttonView).setText("1 min left!");
                } else if (millisUntilFinished < 30000) {
                    map.get(buttonView).setText("30 sec left!");
                } else {
                    map.get(buttonView).setText(millisUntilFinished/60000 + ":" + (millisUntilFinished%60000)/1000);
                }
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                map.get(buttonView).setText("Spawned!");
            }

        }.start();
    } else if (buttonView.isChecked()) {
        // cancel the timer basically
    }
}

Unlike what i originally wanted to do, i've created a textview for each checkbox, so it wouldn't be confusing to the user. I've created a map which contains (Checkbox, TextView). How do i cancel the timer when the checkbox is unchecked? do i make an arraylist? if so, where would i add the timer into the arraylist?

There are all manner of timers and such available to Java / Android. Just looking at the 'related' topics here turns up:

this
this
and so on...

If you want to have multiple timers for your UI you could create a class with the timer and kick off multiple instances - one for each check box event. Keep an array of these objects in your UI class.

You could also have a single timer for the first event and store the deltas for any subsequent check box events.

http://developer.android.com/reference/java/util/Timer.html

Create three timers and simply start them with 5 minutes each when a checkbox is false or unchecked or whatever the property is. Use the schedule method of each timer to do that 1 minute left thing you're talking about.

My main concern is...how do i set a timer on all of them at the same time if i were to uncheck them all at once?

You can use CountdownTimer . Set an onCheckedChangeListener on your CheckBoxes and create a new instance of your CountdownTimer each time.

if i uncheck "checkbox1" and set up a timer for that checkbox, wouldn't unchecking "checkbox2" override that timer?

Not if you create separate instances of the CountdownTimer for each one it shouldn't give you a problem.

You could store these instances of CountdownTimer in an Array if you needed to so you could iterate over them and cancel and restart each one if you needed.

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