我的活動xml中有一個EditText和一個Button。我想要做的是,單擊按鈕時,EditText中的文本應顯示在EditText上方的TextView中。在Android中動態添加TextView
<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"/>
,這是我的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.layout01);
TextView text=new TextView(this);
text.setText(message);
layout.addView(text);
}
當我按一下按鈕,沒有任何反應。一點都沒有。 任何想法,爲什麼這不工作?我對Android開發很陌生。
此,像變魔術一樣,我 – user1509702 2012-07-08 09:53:37