显示正在键入的文本

时间:2013-01-28 05:54:44

标签: java android android-edittext

我正在尝试为Android应用程序开发聊天室。我为EditText创建了一个区域,并为用户键入的文本创建了Enter的相应按钮。

点击Enter我希望在同一屏幕上显示键入的文本,即输入的文本,随后显示在同一屏幕上。我正在使用Linear Layout(Horizontal)作为我的应用。

我该如何实现?有人可以帮我解决这些问题。我是Android开发框架的新手。谢谢和问候。

4 个答案:

答案 0 :(得分:0)

您可以使用'Toast'显示消息或使用另一个'TextView',它使用'setText()'

设置

答案 1 :(得分:0)

非常简单。使用一个textView和一个edittext以及一个按钮创建xml文件。然后在mainActivity中处理按钮单击事件并从中调用onResume。覆盖onResume,以便您可以更新textview。

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    TextView text = (TextView) findViewById(R.id.txtView1);
    EditText editBox = (EditText)findViewById(R.id.txtBox1);
    String str = text.getText().toString();
    text.setText(str+" "+editBox.getText().toString());
    editBox.setText("");
    editBox.setHint("Type in here");

}

答案 2 :(得分:0)

<强> main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent"android:orientation="vertical" android:layout_height="match_parent">
     </LinearLayout>
     <LinearLayout  android:id="@+id/linearLayout2"android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">

      <EditText...
      <Button...
     </LinearLayout>
</LinearLayout>

setContentView(R.Layout.main);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1); //Layout where you want to put your new dynamic TextView.

    String s=editText.getText().toString(); //Fetching String from your EditText
    TextView tv = new TextView(this);
    tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    tv.setText(s);
    ll.addView(tv); //Add TextView inside the Layout.

答案 3 :(得分:0)

您可以使用一个editText进行输入,使用一个TextView显示已键入的消息:

 tvChatWindow = (TextView) findViewById(R.id.tvChatWindow);
 etInputWindow = (EditText) findViewById(R.id.etInputWindow);        
 btnEnter = (Button) findViewById(R.id.btnEnter);

 btnEnter.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
         // send message to other chat clients here

         //add a new line break character and the typed string to Chat Window
         tvChatWindow.append("\n" + etInputWindow.getText().toString());  

         //clear the text you have typed on the edittext
         etInputWindow.setText("");
     }
 });
相关问题