2013-03-06 64 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