清除之前在 EditText 字段中输入的文本
嘿, 我试图让我的 editText 窗口在以下情况下清除其内容: 1)重新选择输入不同的数据或 2) 按下按钮输入预定数据
这不是提示文本。目前,一旦输入预定数据,它就会保留在那里,即使使用删除,我也无法清除它。 我尝试将 editBox.setText("") 放入 onClick Listener 和 addTextChangedListener (之前和 on 方法)中,使用布尔值来确定代码是否已输入,但它只是忽略它。
foodBox.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
addFood();
valueAdded=true;
}
});
dogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (valueAdded==true){
foodBox.setText("");
setFood(0.0);
}
isClicked=true;
animal=1;
addFood();
valueAdded=true;//value in the box
}
});
catButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (valueAdded){
foodBox.setText("");}
isClicked=true;
animal=2;
addFood();
valueAdded=true;
}
});
private void addFood() {
try{
aClass aob = new aClass();
setFood(0.0);
String a = this.foodBox.getText().toString();
if(isClicked&&TextUtils.isEmpty(a)){
if(animal==1){
a=mob.getDog();
}
if(animal==2){
a=mob.getCat();
}
this.foodBox.setText(a);
double d=Double.parseDouble(a);
setFood(d);
}else{
if(TextUtils.isEmpty(a)){
a="0";
}
double d=Double.parseDouble(a);
setFood(d);
}
}
catch (Exception e){
this.showAnswer.setText("Please input animals in numbers");
addFood();
}
}
谢谢
Hey,
I'm trying to get my editText window to clear its contents when either:
1) It is reselected to enter different data or
2) A button is pressed to input predetermined data
This is not hint text. At the moment once the predetermined data is input it stays there and I can't clear it, even using delete.
I have tried putting editBox.setText("") in the onClick Listener and the addTextChangedListener (both before and on methods) using a boolean to determine if code has already been input, but it just ignores it.
foodBox.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
addFood();
valueAdded=true;
}
});
dogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (valueAdded==true){
foodBox.setText("");
setFood(0.0);
}
isClicked=true;
animal=1;
addFood();
valueAdded=true;//value in the box
}
});
catButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (valueAdded){
foodBox.setText("");}
isClicked=true;
animal=2;
addFood();
valueAdded=true;
}
});
private void addFood() {
try{
aClass aob = new aClass();
setFood(0.0);
String a = this.foodBox.getText().toString();
if(isClicked&&TextUtils.isEmpty(a)){
if(animal==1){
a=mob.getDog();
}
if(animal==2){
a=mob.getCat();
}
this.foodBox.setText(a);
double d=Double.parseDouble(a);
setFood(d);
}else{
if(TextUtils.isEmpty(a)){
a="0";
}
double d=Double.parseDouble(a);
setFood(d);
}
}
catch (Exception e){
this.showAnswer.setText("Please input animals in numbers");
addFood();
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
呃...
我用来实现 1) 的唯一代码是:
host
只是一个 EditText。效果好吗?我认为你让事情变得比实际情况更复杂。Errr...
The only code I use to achieve 1) is:
host
just being an EditText. Works just fine? I think you are making this more complicated than it is.单击“清除”按钮时清除:
它对我有用。希望有帮助。
To clear When Clear Button is Clicked:
It Worked For me. Hope It Helps.
1)onClick 对我不起作用,因为我必须按两次才能调用 onClickListener。其他方法是 setOnFocusChangeListener()
For 2) 由ashim888 给出的解决方案效果很好。
For 1) onClick did not work for me as I had to press twice to call onClickListener. Other approach would be setOnFocusChangeListener()
For 2) solution given by ashim888 works well.