[英]How do i add +30 everytime when user click on plus button?
For the first item the total is 49, but I want to add 30 every time the user clicks on the button. 对于第一个项目,总数为49,但是我想在用户每次单击按钮时添加30。
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 Ans会像这样
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. 如果要在单击加号时添加30,则首先需要在Button
上添加OnClickListner
,实现取决于您使用的语言,然后可以突出显示实际代码。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.