所以,我有一個Android程序,像這樣:如何清除Android TextView中的文本?
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
工作完全正常不過的文本不會清除。
我的看法是,TextView
的操作將類似於JTextField
或JTextArea
,其中將文本設置爲""
將清除它。
如何清除文本?
儘量不要混淆'TextView'和'EditText'。一個'EditText'類似於'JTextField' /'JTextArea','TextView'和'JLabel'(以及等效的更大的非用戶可編輯框) – ataulm
@ataulm感謝您提到這個! :-) –