而是附加TextView setText

时间:2012-07-31 21:45:21

标签: android

您好我试图在一个由线程运行的方法中使用.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=" " />

我希望每当我在textview中放一些内容而不是附加内容时就清除它,任何人都知道问题是什么?

3 个答案:

答案 0 :(得分:2)

您将其设置为"txt + ":" + f""txt"被定义为textview中已有的内容。如果您只想在每次调用方法时将其设置为"f",请执行"txtHand.setText(f);"按原样,您要求它采取当前在textview中的任何内容,并添加冒号和{{ 1}}

答案 1 :(得分:1)

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

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

然后使用此行将其添加到:f

txtHand.setText(txt + ":"  + f);

答案 2 :(得分:0)

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

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是

相关问题