EditText : set number of characters programmatically

后端 未结 4 1086
伪装坚强ぢ
伪装坚强ぢ 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 06:50
    public void setEditTextMaxLength(int length) {
        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(length);
        edt_text.setFilters(FilterArray);
    }
    
    0 讨论(0)
  • 2020-12-29 07:00

    Try this my friend easy game easy life InputFilter[] FilterArray = new InputFilter[1]; FilterArray[0] = new InputFilter.LengthFilter(5); input.setFilters(FilterArray);

    0 讨论(0)
  • 2020-12-29 07:06

    You can Use InputFilter for restricting the number of characters in EditView programmatically as:

    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(10);
    your_edittext.setFilters(FilterArray);
    

    for more help you can see this tutorial for restricting number of characters in EditView:

    http://www.tutorialforandroid.com/2009/02/maxlength-in-edittext-using-codes.html

    0 讨论(0)
  • 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});
    
    0 讨论(0)
提交回复
热议问题