Cra*_*oll 5 android textview settext
我有一个文本视图,它本身是线性布局的一部分。XML是:
<TextView
android:id="@+id/label_text"
android:layout_width="fill_parent"
android:layout_height="77dp"
android:layout_marginTop="3dp"/>
Run Code Online (Sandbox Code Playgroud)
在主程序中,我首先将文本视图设置为一个值:
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");
}
Run Code Online (Sandbox Code Playgroud)
这很好用。
然后我执行另一个 setText 以响应按钮按下:
public void handleNextButtonClick(View view) {
character_speaking.setText("first one line message");
}
Run Code Online (Sandbox Code Playgroud)
这也有效
但是后来我又做了一个 setText 来响应另一个按钮的按下:
public void handlePrevButtonClick(View view) {
character_speaking.setText("second one line message");
}
Run Code Online (Sandbox Code Playgroud)
和灾难来袭。第二行消息覆盖第一行消息而不清除它。
在编写第二行消息之前,是否有清除第一行消息的方法?
我已经尝试通过使用 EditText 而不是 TextEdit 来解决这个问题,但这是不可接受的,因为它允许用户在现场书写。
希望这能为您指明正确的方向。
更新 - 工作代码和说明
应用程序最小 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>
Run Code Online (Sandbox Code Playgroud)
我的 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>
Run Code Online (Sandbox Code Playgroud)
最后是我的 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));
}
};
}
Run Code Online (Sandbox Code Playgroud)
抱歉之前的快速回答,但是按下按钮时会产生以下结果。

| 归档时间: |
|
| 查看次数: |
8657 次 |
| 最近记录: |