TextView 上的 setText 将覆盖之前的文本 [英] setText on a TextView will overwrite previous text

查看:38
本文介绍了TextView 上的 setText 将覆盖之前的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本视图,它本身是线性布局的一部分.XML 是:

I have a text view that is by itself as part of a linear layout. The XML is:

<TextView
    android:id="@+id/label_text"
    android:layout_width="fill_parent"
    android:layout_height="77dp"
    android:layout_marginTop="3dp"/>

在主程序中,我首先将文本视图设置为一个值:

In the main program I first set the text view to a value:

private TextView character_speaking;

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

    character_speaking = (TextView) findViewById(R.id.label_text);
    character_speaking.setText("a text string that was large enough for two lines");
}

效果很好.

然后我执行另一个 setText 以响应按钮按下:

Then I do another setText in response to a button push:

public void handleNextButtonClick(View view) {
    character_speaking.setText("first one line message");
}

这也有效

但后来我又做了一个 setText 来响应另一个按钮的按下:

But then I did another setText in response to another button push:

public void handlePrevButtonClick(View view) {
    character_speaking.setText("second one line message");
}

灾难来袭.第二行消息覆盖第一行消息而不清除.

And disaster strikes. The second one line message writes over the first one line message without clearing it.

在写入第二行消息之前,无论如何要清除第一行消息吗?

Is there anyway to clear the first one line message before writing the second one line message?

我已经尝试通过使用 EditText 而不是 TextEdit 来解决这个问题,但这是不可接受的,因为它允许用户在字段中书写.

I have already tried to get around this problem by using an EditText instead of a TextEdit but this was not acceptable because it allowed the user to write in the field.

推荐答案

我遇到过类似的问题.让它工作的原因很有趣 -> 我只是在我的布局 (LinearLayout) 中添加了黑色背景色,现在工作正常.

I've encountered this similar issue. What made it work is funny -> I just put a black background color to my layout (LinearLayout) and it is working fine now.

背景好像没有被清除,如果它不知道用什么颜色清除它.

It seems the background is not cleared, if it doesn't know what color to clear it with.

这篇关于TextView 上的 setText 将覆盖之前的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆