尝试在TextView中打印数学表。它唯一的印刷最后一行表?

时间:2017-03-16 15:13:53

标签: android

  int n = Integer.parseInt(String.valueOf(txt.getText()));

  for(int i = 1; i <= 10; i++)
  {
      txtview.setText(n+" "+i+"s are "+n*i);
  }

输入来自一个由按钮触发的editText。

这是textview:

 <TextView
        android:id="@+id/textView"
        android:layout_width="395dp"
        android:layout_height="264dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Hey! what's up?"
        android:textSize="20dp"
        android:singleLine="false"
        android:inputType="textEmailAddress|textMultiLine"
        android:lines="20"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

2 个答案:

答案 0 :(得分:4)

而不是:

for(int i = 1; i <= 10; i++) {
    txtview.setText(n+" "+i+"s are "+n*i);
}

试试这个:

StringBuilder sBuilder = new StringBuilder();
for(int i = 1; i <= 10; i++) {
    sBuilder.append(n+" "+i+"s are "+n*i);
}
txtview.setText(sBuilder.toString());

答案 1 :(得分:2)

每次你做

setText();

用新东西替换实际文本。使用

setText(txtview.getText().toString()+ "n+" "+i+"s are "+n*i");

代替。

或者只是制作一个大字符串,并在循环后显示:

String result="";
for(...) {
    result+="n+" "+i+"s are "+n*i";
}
txtview.setText(result);