如何在文本视图Android中显示键入的文本

时间:2013-03-06 05:15:46

标签: android android-activity

我在文本视图中显示文本时遇到问题。我想制作一个聊天/发短信风格的应用程序。另外,我如何在我的文本视图字段周围有一个边框,以便它看起来不错。谢谢!我为我的烦恼而道歉。

    <?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">
     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:text="" />
    <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:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendMessage"
        android:text="@string/button_send" 
        android:layout_gravity="bottom"/>

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

  Button button = (Button)findViewById(R.id.button_send);

    };

4 个答案:

答案 0 :(得分:4)

这肯定会有用

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void sendMessage(View v){

    EditText editMessage=(EditText)findViewById(R.id.edit_message);
    TextView textView = (TextView) findViewById(R.id.textView1);

    //get text from edittext and convert it to string
    String messageString=editMessage.getText().toString();

    //set string from edittext to textview
    textView.setText(messageString);

    //clear edittext after sending text to message
    editMessage.setText("");


}

}

答案 1 :(得分:1)

<TextView android:text="" android:background="@drawable/border"/>

在xml中包含上述行并在border.xml文件夹中创建res/drawable并在border.xml

中编写以下代码
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#dfdfdf" />
   <stroke android:width="1dip" android:color="#aa0000"/>
</shape>

这将设置textView

的边框

以后的部分在java中使用它

button.setOnClickListener(new OnClickListener() {
       void onClick() {
         textView.setText(editText.getText());
        }
    });

希望对你有用:) 让我们知道是否有任何问题

答案 2 :(得分:1)

可能你需要这个。

 EditText edt = (EditText)findViewById(R.id.edit_message);
 TextView txt= (TextView)findViewById(R.id.textview1);
 String value = edt.getText().toString();

button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                 txt.append(value+"\n");
                      edt.setText("");
            }
     });

答案 3 :(得分:0)

如果您想从字符串中显示,请使用

 android:text="@strings/text"

如果您想从edittext获取文本并附加到textview,那么

  EditText edit = (EditText) findViewById(R.id.editText);
  TextView text = (TextView) findViewById(R.id.Text);
  String str=edit.getText().toString();
  text.setText(str);