How to delete instantly SPACE from an edittext if a user presses the space?

前端 未结 7 2054
星月不相逢
星月不相逢 2020-12-09 22:44

I have an edittext, and a textwatcher that watches if SPACE arrived or not. If its a SPACE I would like to delete that instantly. Or if its a space I want to make sure it do

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 23:13

    One more simple way to achieve this using the input Filter

    editText.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.toString().equalsIgnoreCase(" ")){
                    return "";
                }
                return source;
            }
        }});
    

    This will remove the space entered by the user immediately and gives appearance like space is disabled.

提交回复
热议问题