0

嗨,我试图在由线程运行的方法中使用 .setText 设置 textview 的值,但它被附加到 TextView 中。参数 String f 是在 for 循环中发送到方法的,如下所示:

    for (int j = 0; j < 5; j++) {
        String[] string = array[j].split(":");
        String y= string[0];
        String x= string[1];
        determineSeat(y, something, x);
    }
}

private void determineSeat(String is, String cs, String f)
{
            TextView txtHand = null;
    String txt = null;

    if (iSeat < cSeat) {
        x = cSeat - iSeat;

        if (x == 1) {
            txtHand = (TextView) findViewById(R.id.txtHand8);
            txt = txtHand.getText().toString();
            txtHand.setText("");
            txtHand.setText(txt + ":"  + f);
        }
}

XML:

<TextView
    android:id="@+id/txtHand1"
    android:layout_width="73dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:text=" " />

我希望每次我在文本视图中放一些东西而不是被附加时都将其清除,有人知道问题是什么吗?

4

3 回答 3

2

您将其设置为"txt + ":" + f". "txt"被定义为文本视图中已经存在的任何内容。如果您只想在"f"每次调用该方法时将其设置为,请按原样执行"txtHand.setText(f);" ,您要求它获取当前文本视图中的任何内容,并添加一个冒号和"f."

于 2012-07-31T21:53:20.843 回答
1

我认为您正在为此 var 分配当前文本:

txt = txtHand.getText().toString();

然后:f用这条线把它放在前面

txtHand.setText(txt + ":"  + f);
于 2012-07-31T21:50:05.807 回答
0

我认为这是因为您每次都在循环内初始化一个新的文本框对象,然后将其内容附加到新的文本框对象。您应该在循环外声明并初始化文本框。

TextView txtHand = (TextView) findViewById(R.id.txtHand8);

for (int j = 0; j < 5; j++) {
        String[] string = array[j].split(":");
        String y= string[0];
        String x= string[1];
        determineSeat(y, something, x);
    }
}

private void determineSeat(String is, String cs, String f)
{
    String txt = null;

    if (iSeat < cSeat) {
        x = cSeat - iSeat;

        if (x == 1) {
            txtHand.setText(":"  + f);
        }
}

字符串是什么,cs是什么

于 2012-07-31T21:51:07.407 回答