setText on a TextView will overwrite previous text

扶醉桌前 提交于 2019-12-12 15:03:10

问题


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");
}

This works well.

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

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

And this worked as well

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?

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.


回答1:


I think that I have decided on a workaround.

Instead of using a TextEdit (which apparently cannot be used for what I want), I will use an EditText.

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

The last line is the key. This prevents the user from clicking on the EditText and defacing the text with a keyboard.

The rest of the code is what you would expect:

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");
}

and everything is working fine.




回答2:


Hope this points you in the right direction.

Updated - Working Code & Description

Application min sdk is 11, and target sdk is 19.

My Previous Answer was a little miss leading because in fact you cannot define a ViewGroup in your XML. So you have to create a basic LinearLayout with a resource Id. Here is the following XML which I have come up with for you.

I hope this is exactly what you were looking for I forgot to note for you that you can't define a ViewGroup in XML, but you have to cast your layout to a 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>

My 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>

And Finally my 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));

    }

};

}

Sorry for the Quick Answer before but this produces the following results when the buttons are pressed.




回答3:


You wanted to clear previous text, right? So you can use an EditText and clear the view with editText.getText().clear().

For not making it clickable just use editText.setFocusable(false) or editText.setClickable(false). I'd rather use setClickable, but try yourself.




回答4:


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.




回答5:


The TextView.setText(String) method will always overwrite the String already stored.

See the documentation for this method here: http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)

If you want to add the previous string and the new string, use:

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



来源:https://stackoverflow.com/questions/22362360/settext-on-a-textview-will-overwrite-previous-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!