如何在 textView 中显示多个值
How to display multiple values in a textView
我无法让 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(值);我可以看到生成的数字,但 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);
}
因此,您要做的是使用 StringBuilder 在循环中构建您的字符串。然后将StringBuilder的值设置为循环后的textView。像这样_:
StringBuilder builder = new StringBuilder();
for (int rolls = 0; rolls < 4 +temp; rolls++) {
value = (int) (Math.random() * 4 + 1);
builder.append(value + " ");
System.out.println(value);
}
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(builder.toString());
// Set the text view as the activity layout
setContentView(textView);
我无法让 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(值);我可以看到生成的数字,但 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);
}
因此,您要做的是使用 StringBuilder 在循环中构建您的字符串。然后将StringBuilder的值设置为循环后的textView。像这样_:
StringBuilder builder = new StringBuilder();
for (int rolls = 0; rolls < 4 +temp; rolls++) {
value = (int) (Math.random() * 4 + 1);
builder.append(value + " ");
System.out.println(value);
}
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(builder.toString());
// Set the text view as the activity layout
setContentView(textView);