Reputation: 1
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$)
Upvotes: 0
Views: 483
Reputation: 44919
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.
Upvotes: 0
Reputation: 21627
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).
Upvotes: 0