Lit*_*ild 6 android android-edittext onclicklistener
所以我有一个像这样的Android程序:
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("");
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
用于向TextView作品添加文本的按钮非常精细,但文本永远不会清除.
我的信念是,操作TextView将类似于JTextField或JTextArea设置文本以""清除它.
如何清除文字?
No_*_*ulz 13
试试这个,
public class AndroidDemo extends Activity implements OnClickListener{
Button add, clear;
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button) findViewById(R.id.addBtn);
clear = (Button) findViewById(R.id.clrBtn);
text = (EditText) findViewById(R.id.display);
add.setOnClickListener(this);
clear.setOnClickListener(this);
}
@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("");
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30586 次 |
| 最近记录: |