So I have an Android program like so:
package com.example.androiddemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class AndroidDemo extends Activity {
String[] messages = {"Short Text",
"I want to show some really long text" +
"on the display of the phone. " +
"Having run out of ideas on what to type, " +
"I am adding this text which makes absolutely " +
"no sense."};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button add = (Button) findViewById(R.id.addBtn);
final Button clear = (Button) findViewById(R.id.clrBtn);
final EditText text = (EditText) findViewById(R.id.display);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v == add){
text.setText(messages[new java.util.Random().nextInt(messages.length)]);
}else if(v == clear){
text.setText("");
}
}
});
}
}
中添加文本的按钮可以很好地工作,但是文本从未被清除。我认为
TextView
的操作类似于 JTextField
或 JTextArea
,在这些组件中将文本设置为""
可以清除其内容。
我如何清空文本?
TextView
和EditText
。EditText
类似于JTextField
/JTextArea
,而TextView
则类似于JLabel
(以及相应的较大的不可编辑框)。 - ataulm