For the first item the total is 49, but I want to add 30 every time the user clicks on the button.
1st item: 49
2nd item: 79
3rd item: 109
and so on.
I have tried this but it's not working:
for (int i =49;i<9;i=i+30)
{
price.setText("$" + Integer.toString(i));
}
if you want to do you work in loop then do it like this.
int firstval = 49;
for (int i = 0; i < 9; i++) {
System.out.println(Integer.toString(firstval += 30));
}
ans will be like this
I/System.out: 79
I/System.out: 109
I/System.out: 139
I/System.out: 169
I/System.out: 199
I/System.out: 229
I/System.out: 259
I/System.out: 289
I/System.out: 319
or if you want to do with button click then
int firstval = 49;
button.setOnClickListener(v -> incValue());
private void incValue() {
System.out.println(Integer.toString(firstval += 30));
}
If you want to add 30 on clicking plus button, first you need to add an OnClickListner
on Button
, the implementation will depend on which language you are using and then the actual code can be highlighted.
You can get the actual value in the listener callback function and then just add your defined value and set it, why you need to use the loop
public static final Integer INC_FACTOR = 30; // Increment factor
public onClickListner(Button ref) { // some dummy onClickListner
ref.setLabel(String.valueOf(Integer.parseInt(ref.getLabel())+ INC_FACTOR));
}
Something like this ?
public class counter {
total = 0;
userClicked() {
total += 30;
}
}
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.