如何在Android中设置EditText的最大字符数?


Answers:


260

在XML中,您可以将此行添加到,EditText View其中140是最大字符数:

android:maxLength="140"

您能否带领我使用Eclipse的xml图形视图中的properties选项卡来实现此目标?
Nitesh Verma

@NiteshVerma在您的res / layout xml文件中,下一个'<LinearLayout...。<EditText android:maxLength =“ 20”'
Shell Scott

60

动态地:

editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(MAX_NUM) });

通过xml:

<EditText
    android:maxLength="@integer/max_edittext_length"

45

您可以使用InputFilter,方法如下:

EditText myEditText = (EditText) findViewById(R.id.editText1);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(10); //Filter to 10 characters
myEditText .setFilters(filters);

7

我总是这样设置最大值:

    <EditText
        android:id="@+id/edit_blaze_it
        android:layout_width="0dp"
        android:layout_height="@dimen/too_high"

    <!-- This is the line you need to write to set the max-->
        android:maxLength="420"
        />

4

它为我工作。

    etMsgDescription.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maximum_character)});

    etMsgDescription.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            tvCharacterLimite.setText(" "+String.valueOf(maximum_character - etMsgDescription.getText().length()));
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

0

它以这种方式为我工作,这是我发现的最好的。最大长度为200个字符

editObservations.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (editObservations.getText().length() >= 201){
                String str = editObservations.getText().toString().substring(0, 200);
                editObservations.setText(str);
                editObservations.setSelection(str.length());
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});

0
   <EditText
        android:id="@+id/edtName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter device name"
        android:maxLength="10"
        android:inputType="textFilter"
        android:singleLine="true"/>

InputType必须设置“ textFilter”

        android:inputType="textFilter"

0

在任何地方轻松使用此功能:

 public static void setEditTextMaxLength(EditText editText, int length) {
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(length);
    editText.setFilters(FilterArray);
  }

0

科特林方式

editText.filters = arrayOf(InputFilter.LengthFilter(maxLength))
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.