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="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

4 回答 4

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("");


}

}
于 2013-03-06T05:39:59.913 回答
1

Probably you need this.

 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("");
            }
     });
于 2013-03-06T05:31:44.803 回答
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());
        }
    });

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

于 2013-03-06T05:27:10.810 回答
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);
于 2013-03-06T05:23:42.870 回答