简体   繁体   English

当用户单击加号按钮时,我如何每次加+30?

[英]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.

相关问题 每次单击按钮将视图添加到Fragment的LinearLayout中 - Add Views to LinearLayout of Fragment everytime I click button 如何在java中按钮单击时迭代添加到List? - How do I iteratively add to a List on button click in java? 如何在单击按钮时将自定义布局添加到线性布局 - how do I add a custom layout to a Linear Layout on the click of a button 如何在按钮单击时添加动态文本字段 - how do add dynamic textfield on button click 当用户单击“ x”按钮时,我可以在Java Swing中做什么以最小化窗口(而不是关闭窗口)? - What can I do in Java Swing to minimize the window (and not to close it) when the user click on the “x” button? Android Studio SQLite 每次单击查看按钮时表都会重复 - Android Studio SQLite table duplicates everytime I click view button 每次单击按钮时,都会在Firebase中创建节点 - Nodes are getting created in Firebase everytime I click a button 如何检查用户何时单击了正确的按钮? - How do I check when a user clicks the correct button? 如何在最后一张幻灯片中单击按钮从许多 ViewPager 幻灯片中收集用户输入? - How do i collect user input from many ViewPager slides at a button click in the last slide? 如何使用htmlunit单击javascript按钮? - How do I click a javascript button with htmlunit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM