在Android中动态添加TextView
我的活动xml中有一个EditText和一个按钮。我想做的是,单击按钮,EditText中的文本将显示在EditText上方的文本视图中在Android中动态添加TextView,android,android-layout,textview,Android,Android Layout,Textview,我的活动xml中有一个EditText和一个按钮。我想做的是,单击按钮,EditText中的文本将显示在EditText上方的文本视图中 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height=
<LinearLayout 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"
android:gravity="bottom"
android:orientation="vertical" >
<EditText
android:id="@+id/edit_message"
android:hint="@string/hint_message"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button
android:id="@+id/button_send"
android:text="@string/button_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="sendMessage"/>
当我点击按钮时,什么也没发生。什么都没有。
你知道为什么这样不行吗?我对Android开发还很陌生。你必须在你的
EditText
上面有一个TextView
,并设置它的属性Android:visibility=“gone”
现在,当您想在该TextView
中显示文本时。执行getText()
从EditText
获取文本,并使用setText()
为TextView
设置文本,同时为TextView
执行setVisibility(View.VISIBLE)
像
您必须在
EditText
上方有一个TextView
,并设置其属性android:visibility=“gone”
现在,当您想在该TextView
中显示文本时。执行getText()
从EditText
获取文本,并使用setText()
为TextView
设置文本,同时为TextView
执行setVisibility(View.VISIBLE)
像
您必须为新视图设置
LayoutParams
(wrap\u content/match\u parent):
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text.setText(message);
layout.addView(text);
您必须为新视图设置
LayoutParams
(wrap\u content/match\u parent):
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text.setText(message);
layout.addView(text);
将此行添加到LinearLayout标记
android:id="@+id/linear"
并将sendMessage方法更改为
public void sendMessage(View view){
EditText editText=(EditText)findViewById(R.id.edit_message);
String message=editText.getText().toString();
LinearLayout layout=(LinearLayout)findViewById(R.id.linear);
TextView text=new TextView(this);
text.setText(message);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(text);
}将此行添加到LinearLayout标记中
android:id="@+id/linear"
并将sendMessage方法更改为
public void sendMessage(View view){
EditText editText=(EditText)findViewById(R.id.edit_message);
String message=editText.getText().toString();
LinearLayout layout=(LinearLayout)findViewById(R.id.linear);
TextView text=new TextView(this);
text.setText(message);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(text);
}将XML更改为以下格式
<LinearLayout 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"
android:gravity="bottom"
android:id="@+id/linear"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<EditText
android:id="@+id/edit_message"
android:hint="@string/hint_message"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button
android:id="@+id/button_send"
android:text="@string/button_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="sendMessage"/>
}将XML更改为以下格式
<LinearLayout 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"
android:gravity="bottom"
android:id="@+id/linear"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<EditText
android:id="@+id/edit_message"
android:hint="@string/hint_message"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button
android:id="@+id/button_send"
android:text="@string/button_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="sendMessage"/>
}如果我每次单击按钮都想显示新的文本视图,那么这是否可行?对于每次新的文本视图,我的第一个答案将以这种方式工作。这将每次在上一个文本视图上设置新文本。这是否可行?如果我想每次单击按钮都显示新文本视图??对于每次新文本视图,我的第一个答案将以这种方式工作。这将每次在上一个文本视图上设置新文本。