[英]Add Views to LinearLayout of Fragment everytime I click button
[英]How do i add +30 everytime when user click on plus button?
對於第一個項目,總數為49,但是我想在用戶每次單擊按鈕時添加30。
1st item: 49
2nd item: 79
3rd item: 109
等等。
我已經嘗試過了,但是沒有用:
for (int i =49;i<9;i=i+30)
{
price.setText("$" + Integer.toString(i));
}
如果您想做循環工作,就可以這樣做。
int firstval = 49;
for (int i = 0; i < 9; i++) {
System.out.println(Integer.toString(firstval += 30));
}
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
或者如果您想使用按鈕單擊,則
int firstval = 49;
button.setOnClickListener(v -> incValue());
private void incValue() {
System.out.println(Integer.toString(firstval += 30));
}
如果要在單擊加號時添加30,則首先需要在Button
上添加OnClickListner
,實現取決於您使用的語言,然后可以突出顯示實際代碼。
您可以在偵聽器回調函數中獲取實際值,然后只需添加定義的值並進行設置即可,為什么需要使用循環
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));
}
像這樣嗎?
public class counter {
total = 0;
userClicked() {
total += 30;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.