简体   繁体   中英

Pass the name of button clicked to different class in Android

I am new to android (and not so hot with it, but trying to learn).

I am creating an application that has a number of various buttons that start countdown timers when they are clicked.

On the activity that has these buttons the following code is used to start the timer:

    //Button 1 Start On Click
    final CountDown buttonOneTimer = new CountDown(15000,1000);
    buttonOne.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonOneTimer.start();
        }
    });

    //Button 2 Start On Click
    final CountDown buttonTwoTimer = new CountDown(15000,1000);
    buttonTwo.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonTwoTimer.start();
        }
    });

my CountDown class looks like this:

public class CountDown extends CountDownTimer {


    public CountDown (long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }


    @Override
    public void onFinish() {
        System.out.println("Timer Completed.");
        ****NAMEOFTHEBUTTONCLICKED****.setText("Timer Completed.");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        ****NAMEOFTHEBUTTONCLICKED****.setText((millisUntilFinished/1000)+"");
        System.out.println("Timer  : " + (millisUntilFinished/1000));
    }


}

I am trying to get the name of the button pressed into the class so I can set the text on the button to countdown.

I'm sure I am doing all sorts of things wrong or there may be better ways to do it - so if you see areas where I could improve - feel free to critique me!

I read over the tutorial here however it has an 'inner class' (I believe that is what it is called?) inside the current class. A friend of mine said that's very rarely done and to just create a separate class such as CountDown. If I do it the same way as in the tutorial I can get the timer to work (by hardcoding the buttons name where it says * NAMEOFTHEBUTTONCLICKED * above, which means it only works for that button) - but I still need to figure out how to pass that class the buttons name so I don't have to write a separate class for each timer.

Would that be done through Extras? I have had a hard time finding any more info to my specific issue. Any help is appreciated!!

Try passing off the button instance to the Timer:

public class CountDown extends CountDownTimer {

    Button button;
    public CountDown (long millisInFuture, long countDownInterval, Button button) {
        super(millisInFuture, countDownInterval);
        this.button = button;
    }


    @Override
    public void onFinish() {
        System.out.println("Timer Completed.");
        button.setText("Timer Completed.");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        button.setText((millisUntilFinished/1000)+"");
        System.out.println("Timer  : " + (millisUntilFinished/1000));
    }

}

Then call like this:

//Button 1 Start On Click
    final CountDown buttonOneTimer = new CountDown(15000,1000,buttonOne);
    buttonOne.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonOneTimer.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