2013-03-06 65 views
0

我正在嘗試在Android應用中顯示文本,但我對Android開發完全陌生。我很瞭解Java,但是,如何在應用程序的中間顯示文本,就像它是一個短信應用程序一樣?我有一個發送按鈕,但他們點擊後發送底部的文本字段,我希望能夠顯示他們在中間鍵入的內容。顯示打字文本

<?xml version="1.0" encoding="utf-8"?> 
<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:orientation="horizontal"> 
    <EditText android:id="@+id/edit_message" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_message" 
     android:layout_gravity="bottom" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="sendMessage" 
     android:text="@string/button_send" 
     android:layout_gravity="bottom"/> 

</LinearLayout> 
+0

只需添加一個新的TextView,並在每次單擊該按鈕時添加該TextView。 – ElefantPhace 2013-03-06 01:51:50

+0

我該怎麼做?對不起,我對Android開發很陌生(我剛剛開始20分鐘前..).. – 2013-03-06 01:53:39

回答

0

這是你想要的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<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:orientation="vertical"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="400dp" 
     android:text="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="wrap_content" 
     android:orientation="horizontal"> 

     <EditText 
      android:id="@+id/edit_message" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:hint="@string/edit_message" > 

      <requestFocus /> 
     </EditText> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/button_send" /> 
    </LinearLayout> 
</LinearLayout> 

你不一定需要設置你的onClick事件XML格式。使用其他答案作爲示例如何添加onClickListener

+0

謝謝兄弟!你真棒! – 2013-03-06 02:41:59

0

添加一個新的TextView,XML如下:

<TextView 
    android:id="@+id/new_text_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="I am some new text!" /> 

這將只需要創建一個新的TextView元素,你將需要新的參數添加到XML來指定在屏幕上的位置,全部取決於您使用的佈局類型。

如果您將此元素添加到您的Button

android:id="@+id/button" 

您可以編輯從活動中的文字,按下按鈕,使用下面的代碼:

Button button = (Button)findViewById(R.id.button); 
button.setOnClickListener(newView.OnClickListener(){ 
    public void onClick(){ 
     TextView newTextView = (TextView)findViewById(R.id.new_text_view); 
     EditText editMessage = (EditText)findViewById(R.id.edit_message); 
     newTextView.setText(editMessage.getText().toString()); 
    } 
}); 
+0

好吧,那麼我需要創建一個方法,添加點擊發送按鈕時輸入的內容? – 2013-03-06 01:56:23

+0

是的,您可以使用它的ID在Java代碼中引用此元素,然後編輯包含的文本。我將添加上面的代碼。 – 2013-03-06 01:57:12

+0

我編輯了我的上面的代碼,它現在允許您在EditText中輸入文本,然後單擊該按鈕,您必須將新TextView中的文本更改爲您在EditText中輸入的內容。 – 2013-03-06 02:01:52

0

編輯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:orientation="vertical"> 

<TextView ndroid:id="@+id/text_message" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
<EditText android:id="@+id/edit_message" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:hint="@string/edit_message" 
    android:layout_gravity="bottom" 
    /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="sendMessage" 
    android:text="@string/button_send" 
    android:layout_gravity="bottom"/> 
</LinearLayout> 

並在代碼中寫下:

button.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View v) 
    { 
      edit_message= (TextView) findViewById(R.id.edit_message); 
      String text = edit_message.getText().toString(); 
      text_message= (TextView) findViewById(R.id.text_message); 
      text_message.setText(text); 
    } 
} 
+0

這是錯誤的。你甚至檢查過嗎? – ElefantPhace 2013-03-06 02:05:45

+0

它是什麼部分? – 2013-03-06 02:09:48

+0

這兩個部分。 xml是錯誤的,你應該測試一下。並且在xml – ElefantPhace 2013-03-06 02:13:50