AndroidでEditTextの最大文字数をどのように設定しますか?


回答:


260

XMLでは、次の行をEditText View140が最大文字数である場所に追加できます。

android:maxLength="140"

Eclipseのxmlグラフィカルビューの[プロパティ]タブを使用してこれをどのように達成できるかを教えてください。
Nitesh Verma 2013年

res / layout xmlファイル内の@NiteshVermaは、次の '<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))
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.