2012-07-08 83 views
0

我的活動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開發很陌生。

回答

1

您必須在EditText之上有TextView並設置其屬性android:visibility="gone"

現在,當你想在那個時候顯示你的文字TextView。請getText()EditText獲取文本,並使用setText()爲您的TextView設置文本,並同時爲您的TextViewsetVisibility(View.VISIBLE)設置文本。

一樣,

String text = editText.getText(); 
textView.setText(text); 
textView.setVisibility(View.VISIBLE); 
0

您必須設置LayoutParams(WRAP_CONTENT/match_parent)爲您的新景觀:

TextView text = new TextView(this); 

text.setLayoutParams(new LinearLayout.LayoutParams(
     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  

text.setText(message); 
layout.addView(text); 
+0

此,像變魔術一樣,我 – user1509702 2012-07-08 09:53:37

2

此行添加到您的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); 

}

1

你的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"/> 

現在你的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=(TextView)findViewById(R.id.tv); 
text.setText(message); 

}

+0

馬克接受以及答案:)。謝謝。 – Waqas 2012-07-08 09:51:24

+0

這個工作如果我想每次點擊按鈕時顯示新的TextView?每次爲新的TextView提供 – user1509702 2012-07-08 09:52:41

+0

,我的第一個答案將以這種方式工作。這將在每次前面的文本視圖中設置新的文本。 – Waqas 2012-07-08 10:33:04