gpt4 book ai didi

java - 如何在 textView 中显示多个值

转载 作者:行者123 更新时间:2023-11-29 20:15:36 24 4
gpt4 key购买 nike

我无法让 String Builder 为我正常工作,我知道生成的值有效但只有最后一个被添加到 textView 中。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);

Intent newGame = getIntent();
int temp = newGame.getIntExtra("int_value", 0); // here 0 is the default value

Random r = new Random();
int Low = 1;
int High = 4;
int Result = r.nextInt(High-Low) +Low;

int[] numbers = new int [1];
for(int Generated = 0; Generated < numbers.length; Generated++) {
numbers[Generated] = (int)(Math.random() +temp);
}
int value;
for (int rolls = 0; rolls < 4 +temp; rolls++) {
value = (int) (Math.random() * 4 + 1);
//Test if numbers generated are correct
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
StringBuilder builder = new StringBuilder();
builder.append(value + " ");
textView.setText(builder.toString());


// Set the text view as the activity layout
setContentView(textView);
//store generated numbers here
System.out.println(value);
}
}

在System.out.println(value)中;我可以看到生成的数字,但 textView 只显示最后一个。

最佳答案

尝试这样编辑:

StringBuilder builder = new StringBuilder();

for (int rolls = 0; rolls < 4 +temp; rolls++) {
value = (int) (Math.random() * 4 + 1);
//Test if numbers generated are correct
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
builder.append(value + " ");
textView.setText(builder.toString());

// Set the text view as the activity layout
setContentView(textView);
//store generated numbers here
System.out.println(value);
}

关于java - 如何在 textView 中显示多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33958122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com