【问题标题】:EditText : set number of characters programmaticallyEditText:以编程方式设置字符数
【发布时间】:2012-11-16 10:18:35
【问题描述】:

我有一个EditText,我想限制可以在这个EditText中输入的字符数,并以编程方式进行此限制,怎么做?例如,假设我想限制它只允许 10 个字符。

【问题讨论】:

标签: android android-layout android-intent


【解决方案1】:

您可以使用InputFilter 以编程方式限制 EditView 中的字符数:

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

如需更多帮助,您可以查看本教程以限制 EditView 中的字符数:

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

【讨论】:

    【解决方案2】:

    我会实现一个过滤器:

    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});
    

    【讨论】:

      【解决方案3】:
      public void setEditTextMaxLength(int length) {
          InputFilter[] FilterArray = new InputFilter[1];
          FilterArray[0] = new InputFilter.LengthFilter(length);
          edt_text.setFilters(FilterArray);
      }
      

      【讨论】:

        【解决方案4】:

        试试这个我的朋友简单的游戏简单的生活 InputFilter[] FilterArray = new InputFilter[1]; FilterArray[0] = new InputFilter.LengthFilter(5); input.setFilters(FilterArray);

        【讨论】:

          猜你喜欢
          • 2011-11-10
          • 2016-11-10
          • 2017-12-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-30
          • 1970-01-01
          • 2014-03-14
          相关资源
          最近更新 更多