IT박스

Android에서 EditText의 최대 문자 수를 어떻게 설정합니까?

itboxs 2020. 7. 16. 19:48
반응형

Android에서 EditText의 최대 문자 수를 어떻게 설정합니까?


Android EditText 입력에 대한 최대 문자 수를 어떻게 설정합니까? setMaxLines, setMaxEMS가 표시되지만 문자 수에는 영향이 없습니다.


XML EditText View에서 140을 최대 문자 수인 곳에이 행을 추가 할 수 있습니다 .

android:maxLength="140"

동적으로 :

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

xml을 통해 :

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

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

도움이 될 수 있습니다.


나는 항상 다음과 같이 최대 값을 설정했습니다.

    <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"
        />

나를 위해 일하고 있습니다.

    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

        }
    });

그것은 이런 식으로 저에게 효과적이었습니다. 내가 찾은 최고입니다. 최대 길이는 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) {
    }
});

   <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"

이 기능을 어디서나 쉽게 사용할 수 있습니다 :

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

참고URL : https://stackoverflow.com/questions/6066212/how-do-you-set-the-max-number-of-characters-for-an-edittext-in-android

반응형