Android:TextView上的setText将覆盖以前的文本

问题描述 投票:3回答:4

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

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

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

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以响应按钮按下:

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

这也很有效

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

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

和灾难来袭。第二行消息在第一行消息上写入而不清除它。

在写第二行信息之前,是否有清除第一行信息?

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

提前致谢。

android textview settext
4个回答
0
投票

我认为我已经决定了解决方法。

我将使用EditText而不是使用TextEdit(显然不能用于我想要的东西)。

<EditText
    android:id="@+id/label_text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:hint=""
    android:focusable="false"/>

最后一行是关键。这可以防止用户单击EditText并使用键盘来破坏文本。

代码的其余部分是您所期望的:

private EditText character_speaking;

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

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

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

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

一切都很好。


0
投票

希望这能指出你正确的方向。

更新 - 工作代码和说明

应用程序min sdk为11,目标sdk为19。

我的上一个答案有点失误,因为事实上你无法在XML中定义ViewGroup。因此,您必须使用资源ID创建基本LinearLayout。以下是我为您提供的以下XML。

我希望这正是您所寻找的内容我忘记为您注意到您无法在XML中定义ViewGroup,但您必须将您的布局转换为ViewGroup。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/textViewHolder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="vertical" >
</LinearLayout>

<LinearLayout
    android:id="@+id/buttonBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" 
    style="?android:attr/buttonBarStyle">

    <Button
        android:id="@+id/buttonPrevious"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="@string/text_Previous"
        style="?android:attr/buttonBarButtonStyle" />

    <Button
        android:id="@+id/buttonNext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="@string/text_Next"
        style="?android:attr/buttonBarButtonStyle"/>
</LinearLayout>

我的strings.xml:

<?xml version="1.0" encoding="utf-8"?>
 <resources>
  <string name="app_name">TextViewSwitcher</string>
  <string name="text_Previous">Previous</string>
  <string name="text_Next">Next</string>
 </resources>

最后我的MainActivity.class:

public class MainActivity extends Activity {

// ------------------------------------
// Member Variables

private LinearLayout m_textViewHolder = null;
private Button m_btnPrevious = null;
private Button m_btnNext = null;
private final String m_nothingText = "Nothing Pressed";
private final String m_previousText = "Previous Pressed";
private final String m_nextText = "Next Pressed";


// ------------------------------------
// Class Overrides

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

    // Get a reference to the UI Elements
    m_textViewHolder    = (LinearLayout) findViewById(R.id.textViewHolder);
    m_btnPrevious       = (Button) findViewById(R.id.buttonPrevious);
    m_btnNext           = (Button) findViewById(R.id.buttonNext);

    // Add Listeners to the Buttons
    m_btnPrevious.setOnClickListener(PreviousListener);
    m_btnNext.setOnClickListener(NextListener);

    // Populate the Initial Layout with a new TextView, & set the Text
    TextView nothingTextView = new TextView(getBaseContext());
    nothingTextView.setText(m_nothingText);
    ((ViewGroup) m_textViewHolder).addView(nothingTextView);

}

// -----------------------------------------
// Private Methods

// Creates  a new TextView based on the Text Passed in, and returns the new TextView
private TextView createNewTextView(String text)
{
    TextView newTextView = new TextView(getBaseContext());
    newTextView.setText(text);

    return newTextView;
}

// Removes the Child Views of the Parent ViewGroup
private void removeChildViewsFromParent()
{
    ((ViewGroup) m_textViewHolder).removeAllViews();
}

// -------------------------------------
// Listeners

private OnClickListener PreviousListener = new OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
        removeChildViewsFromParent();
        ((ViewGroup) m_textViewHolder).addView(createNewTextView(m_previousText));

    }

};

private OnClickListener NextListener = new OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
        removeChildViewsFromParent();
        ((ViewGroup) m_textViewHolder).addView(createNewTextView(m_nextText));

    }

};

}

对不起之前的快速回答,但按下按钮会产生以下结果。


0
投票

你想清除以前的文字,对吧?因此,您可以使用EditText并使用editText.getText().clear()清除视图。

为了不使它可点击,只需使用editText.setFocusable(false)editText.setClickable(false)。我宁愿使用setClickable,但试试自己。


-1
投票

TextView.setText(String)方法将始终覆盖已存储的String。

请参阅此方法的文档:http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)

如果要添加上一个字符串和新字符串,请使用:

TextView.setText(TextView.getText().toString() + String)

© www.soinside.com 2019 - 2024. All rights reserved.