Question

I have a variable that is an integer array (intArr) containing three minute values. Then I have three edit boxes (e1, e2, e3), where e1 takes value intArr[0], e2 takes value intArr[1] and e3 takes value intArr[2]. I also have a radio button (one can either choose "Show edit box time in minutes" or "Show edit box time in seconds") connected to a variable.

How do I change the time in the edit boxes (which initially are in minutes) into hours, or in the opposite way from hours to minutes depending on which radio button has been chosen?

Was it helpful?

Solution

First, you need to check the value of the radio button that has been selected: You can either use:

getText() or isSelected().

Then, depending on which one was selected, you can set the text of the edit box. However, you need to get the value of the text field first.

You can use:

textField.getText();

Then you can parse it into an int using:

Integer.parseInt(textField.getText());

Finally, you want to divide it by 60 and get the remainder of the divison:

int hours = Integer.parseInt(textField.getText()) / 60; 
int minutes = Integer.parseInt(textField.getText()) % 60;

You will also need to set the textfield to that new value:

textField.setText(hours + ":" + minutes);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top