I'd like to watch out for the amount of the symbols in EditText. To do this, I use
public void onTextChanged(CharSequence s, int start, int before, int count) {
int symbolRemains = 140 - edTextSMS.getText().length();
if (symbolRemains>=0) {
tvSymbolsRemains.setText("Осталось символов:"+symbolRemains);
}
else
{
Toast.makeText(MainActivity.this, "Максимум 140 символов в сообщени开发者_开发知识库и", Toast.LENGTH_SHORT).show();
return;
}
}
});
So, in my ELSE section I'd like to reject the entering symbols. Of course, simple "return" doesn't work. How can I do this ?
Thanks$)
add a text watcher on our edit text and if the max limit was reached, delete the entered character in method onTextChanged (I think you must delete characters from start to start + count, start and count being parameters for this method).
You can do:
StringBuffer buffer = new StringBuffer(s);
buffer.delete(start, start + count);
edTextSMS.setText(buffer.toString());
if you want to limit your EditText
to 140 characters.
Updated with gabi's suggestion.
精彩评论