EditText : set number of characters programmatically

后端 未结 4 1089
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 06:26

I have a EditText , and I would like to restrict the number of characters which could be inputted in this EditText, and make this

4条回答
  •  被撕碎了的回忆
    2020-12-29 07:12

    I would implement a filter:

    InputFilter filter = new InputFilter() {
    
       @Override
       public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
          if (source.length > 10){
           //cancel the edit or whatever
          }
       }
    };
    EditText editText = (EditText) findViewById(R.id.rg);
    editText.setFilters(new InputFilter[] {filter});
    

提交回复
热议问题