添加 setText() 时强制关闭
以下是我的代码:
name = (EditText)findViewById(R.id.editText1);
name.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
name.setText(s.toString()+"-");
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){ }
public void onTextChanged(CharSequence s, int start, int before, int count){ }
});
但是我让应用程序强制关闭,当删除 setText() 时应用程序工作正常
the following is my code :
name = (EditText)findViewById(R.id.editText1);
name.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
name.setText(s.toString()+"-");
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){ }
public void onTextChanged(CharSequence s, int start, int before, int count){ }
});
But I get the app to force close , when remove setText() the app works fine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您陷入了
afterTextChanged
方法中,因为在您setText
之后,它被再次调用,一次又一次......您必须找到另一种方法,如何/何时附加“-”字符。You are getting stuck in your
afterTextChanged
method, because right after yousetText
, it is called again, and again, and again... You have to find another way, how/when to append the "-" character.请检查我不久前问过的这个问题。这很像你的问题:
Changing text in Android on文本更改导致溢出错误
Please check this question I asked some time ago. It's a lot like your question:
Changing text in Android on text change causes overflow error