What's the best way to limit text length of EditText in Android

后端 未结 22 2106
囚心锁ツ
囚心锁ツ 2020-11-22 13:33

What\'s the best way to limit the text length of an EditText in Android?

Is there a way to do this via xml?

22条回答
  •  野性不改
    2020-11-22 14:26

    //Set Length filter. Restricting to 10 characters only
    editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_LENGTH)});
    
    //Allowing only upper case characters
    editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
    
    //Attaching multiple filters
    editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_LENGTH), new InputFilter.AllCaps()});
    

提交回复
热议问题