setText for edittext causing crash in addTextChangedListener
Im trying to strip all non standard letter characters from an edittext textbox on an android app im making. I am successfully creating a listener, obtaining the value and removing bad chrs via a regex. however, the .setText line below causes the app to crash. Anyone got any ideas how to get around this and dynamically mask certain chrs?
filenameTextBox.addTextChangedListener(new TextWatcher() {
public voi开发者_StackOverflowd onTextChanged(CharSequence s, int start, int before, int count) {
FILENAME=s.toString();
FILENAME = FILENAME.replaceAll("[^a-zA-Z]", "");
filenameTextBox.setText(FILENAME);
}
}
Hope this snippet will help you if i understand your problem clearly
public void afterTextChanged(Editable editable)
{
if (editable.length() != 0)
{
chatTextArea.removeTextChangedListener(this);
chatTextArea.setText("your text");
chatTextArea.addTextChangedListener(this);
}
}
Aren't you creating an infinite loop? You can log each time the handler is called and see how many times it's called.
public void onTextChanged(CharSequence s, int start, int before, int count)
{
chatTextArea.removeTextChangedListener(this);
String s_new = s.toString().replaceAll("[^0-9]", ""); // for example, if need
chatTextArea.setText(s_new);
chatTextArea.setSelection(start + count + s_new.length() - s.length());
chatTextArea.addTextChangedListener(this);
}
精彩评论