开发者

How to reject symbols in EditText widget (Android) after some amount of them was entered?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜