setText() 之后无法清除编辑文本
这是我的代码,但问题是当我想清除一些字符时我不能
TWL=new TextWatcher(){
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
name.removeTextChangedListener(this);//after this line you do the editing code
name.setText(s+"-");
name.setSelection(name.getText().length());
name.addTextChangedListener(TWL); // you register again for listener callbacks
}};
name = (EditText)findViewById(R.id.editText1);
name.addTextChangedListener(TWL);
this is my code , but the problem is when the I want to clear some chars i cannot
TWL=new TextWatcher(){
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
name.removeTextChangedListener(this);//after this line you do the editing code
name.setText(s+"-");
name.setSelection(name.getText().length());
name.addTextChangedListener(TWL); // you register again for listener callbacks
}};
name = (EditText)findViewById(R.id.editText1);
name.addTextChangedListener(TWL);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我经历过一个非常类似的问题。我有一个带有 OnKeyListener 的 EditText,但它不允许我删除 edittext 中的任何空格(我没有监听任何“删除按钮”事件)。
当我尝试在侦听器中返回“false”(事件未完成)时,我发现出了什么问题。不是它像魅力一样起作用。你确定你没有任何重要的听众吗?那么,当你“抓取”事件时,你会返回 false 吗?
希望有帮助
I've experienced a quite similar problem. I had a EditText with a OnKeyListener, but it did not let me delete any space in the edittext (I didn't listen to any "delete button" event).
I figured out what's wrong when I tried to return "false" in my listener (event not completed). Not it works like a charm. Are you sure you don't have any key listener on that? In that case, do you return false when you "grab" the event?
Hope it helps