developer tip

Android에서 EditText의 텍스트 길이 제한

optionbox 2020. 10. 2. 22:04
반응형

Android에서 EditText의 텍스트 길이 제한


EditTextAndroid에서 의 텍스트 길이를 제한하는 가장 좋은 방법은 무엇입니까 ?

xml을 통해 이것을 수행하는 방법이 있습니까?


선적 서류 비치

android:maxLength="10"

입력 필터를 사용하여 텍스트보기의 최대 길이를 제한하십시오.

TextView editEntryView = new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(8);
editEntryView.setFilters(filterArray);

EditText editText = new EditText(this);
int maxLength = 3;    
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});

이미 사용자 정의 입력 필터를 사용하는 사람에 대한 참고 또한 최대 길이를 제한하려면 :

코드에 입력 필터를 할당하면 이전에 설정된 모든 입력 필터가 지워집니다 android:maxLength. 암호 필드에서 허용하지 않는 일부 문자의 사용을 방지하기 위해 사용자 지정 입력 필터를 사용하려고 할 때 이것을 발견했습니다. setFilters로 해당 필터를 설정 한 후 maxLength가 더 이상 관찰되지 않았습니다. 해결책은 maxLength와 맞춤 필터를 프로그래밍 방식으로 함께 설정하는 것이 었습니다. 이 같은:

myEditText.setFilters(new InputFilter[] {
        new PasswordCharFilter(), new InputFilter.LengthFilter(20)
});

TextView tv = new TextView(this);
tv.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(250) });

이것을 달성하는 방법을 궁금해하는 다른 사람을 위해 여기 내 확장 EditText클래스가 EditTextNumeric있습니다.

.setMaxLength(int) -최대 자릿수 설정

.setMaxValue(int) -최대 정수 값 제한

.setMin(int) -최소 정수 값 제한

.getValue() -정수 값 얻기

import android.content.Context;
import android.text.InputFilter;
import android.text.InputType;
import android.widget.EditText;

public class EditTextNumeric extends EditText {
    protected int max_value = Integer.MAX_VALUE;
    protected int min_value = Integer.MIN_VALUE;

    // constructor
    public EditTextNumeric(Context context) {
        super(context);
        this.setInputType(InputType.TYPE_CLASS_NUMBER);
    }

    // checks whether the limits are set and corrects them if not within limits
    @Override
    protected void onTextChanged(CharSequence text, int start, int before, int after) {
        if (max_value != Integer.MAX_VALUE) {
            try {
                if (Integer.parseInt(this.getText().toString()) > max_value) {
                    // change value and keep cursor position
                    int selection = this.getSelectionStart();
                    this.setText(String.valueOf(max_value));
                    if (selection >= this.getText().toString().length()) {
                        selection = this.getText().toString().length();
                    }
                    this.setSelection(selection);
                }
            } catch (NumberFormatException exception) {
                super.onTextChanged(text, start, before, after);
            }
        }
        if (min_value != Integer.MIN_VALUE) {
            try {
                if (Integer.parseInt(this.getText().toString()) < min_value) {
                    // change value and keep cursor position
                    int selection = this.getSelectionStart();
                    this.setText(String.valueOf(min_value));
                    if (selection >= this.getText().toString().length()) {
                        selection = this.getText().toString().length();
                    }
                    this.setSelection(selection);
                }
            } catch (NumberFormatException exception) {
                super.onTextChanged(text, start, before, after);
            }
        }
        super.onTextChanged(text, start, before, after);
    }

    // set the max number of digits the user can enter
    public void setMaxLength(int length) {
        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(length);
        this.setFilters(FilterArray);
    }

    // set the maximum integer value the user can enter.
    // if exeeded, input value will become equal to the set limit
    public void setMaxValue(int value) {
        max_value = value;
    }
    // set the minimum integer value the user can enter.
    // if entered value is inferior, input value will become equal to the set limit
    public void setMinValue(int value) {
        min_value = value;
    }

    // returns integer value or 0 if errorous value
    public int getValue() {
        try {
            return Integer.parseInt(this.getText().toString());
        } catch (NumberFormatException exception) {
            return 0;
        }
    }
}

사용 예 :

final EditTextNumeric input = new EditTextNumeric(this);
input.setMaxLength(5);
input.setMaxValue(total_pages);
input.setMinValue(1);

에 적용되는 다른 모든 메서드와 속성 EditText도 물론 작동합니다.


goto10의 관찰로 인해 최대 길이를 설정하여 다른 필터가 손실되지 않도록 보호하기 위해 다음 코드를 작성했습니다.

/**
 * This sets the maximum length in characters of an EditText view. Since the
 * max length must be done with a filter, this method gets the current
 * filters. If there is already a length filter in the view, it will replace
 * it, otherwise, it will add the max length filter preserving the other
 * 
 * @param view
 * @param length
 */
public static void setMaxLength(EditText view, int length) {
    InputFilter curFilters[];
    InputFilter.LengthFilter lengthFilter;
    int idx;

    lengthFilter = new InputFilter.LengthFilter(length);

    curFilters = view.getFilters();
    if (curFilters != null) {
        for (idx = 0; idx < curFilters.length; idx++) {
            if (curFilters[idx] instanceof InputFilter.LengthFilter) {
                curFilters[idx] = lengthFilter;
                return;
            }
        }

        // since the length filter was not part of the list, but
        // there are filters, then add the length filter
        InputFilter newFilters[] = new InputFilter[curFilters.length + 1];
        System.arraycopy(curFilters, 0, newFilters, 0, curFilters.length);
        newFilters[curFilters.length] = lengthFilter;
        view.setFilters(newFilters);
    } else {
        view.setFilters(new InputFilter[] { lengthFilter });
    }
}

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

나는이 문제가 있었고 이미 설정된 필터를 잃지 않고 프로그래밍 방식으로 이것을 수행하는 잘 설명 된 방법을 놓치고 있다고 생각합니다.

XML로 길이 설정 :

수락 된 대답이 올바르게 언급했듯이 나중에 더 이상 변경하지 않을 EditText에 고정 길이를 정의하려면 EditText XML에서 정의하십시오.

android:maxLength="10" 

프로그래밍 방식으로 길이 설정

프로그래밍 방식으로 길이를 설정하려면 InputFilter.

Java의 문제 는 다음과 같습니다. 일단 설정하면 다른 모든 필터 (예 : maxLines, inputType 등)가 사라집니다. 이전 필터를 잃지 않으려면 이전에 적용된 필터를 가져 와서 maxLength를 추가하고 다음과 같이 필터를 다시 EditText로 설정해야합니다.

InputFilter[] editFilters = editText.getFilters();
InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
newFilters[editFilters.length] = new InputFilter.LengthFilter(10); //the desired length
editText.setFilters(newFilters);

그러나 Kotlin을 사용 하면 모든 사람이 더 쉽게 사용할 수 있으며 기존 필터에 필터를 추가해야하지만 다음과 같이 간단하게 수행 할 수 있습니다.

editText.filters += InputFilter.LengthFilter(maxLength)

XML

android:maxLength="10"

자바:

InputFilter[] editFilters = editText.getFilters();
InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
newFilters[editFilters.length] = new InputFilter.LengthFilter(maxLength);
editText.setFilters(newFilters);

Kotlin :

editText.filters += InputFilter.LengthFilter(maxLength)

이를 달성 할 수있는 또 다른 방법은 XML 파일에 다음 정의를 추가하는 것입니다.

<EditText
    android:id="@+id/input"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:maxLength="6"
    android:hint="@string/hint_gov"
    android:layout_weight="1"/>

이렇게하면 EditText위젯 의 최대 길이 가 6 자로 제한됩니다.


에서 material.io , 당신은 사용할 수 TextInputEditText와 함께 TextInputLayout:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:counterEnabled="true"
    app:counterMaxLength="1000"
    app:passwordToggleEnabled="false">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/edit_text"
        android:hint="@string/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLength="1000"
        android:gravity="top|start"
        android:inputType="textMultiLine|textNoSuggestions"/>

</com.google.android.material.textfield.TextInputLayout>

드로어 블을 사용하여 비밀번호 EditText를 구성 할 수 있습니다.

비밀번호 예

또는 카운터를 사용하거나 사용하지 않고 텍스트 길이를 제한 할 수 있습니다.

카운터 예

의존:

implementation 'com.google.android.material:material:1.1.0-alpha02'

XML

android:maxLength="10"

프로그래밍 방식 :

int maxLength = 10;
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(maxLength);
yourEditText.setFilters(filters);

참고 : 내부적으로 EditText 및 TextView android:maxLength는 XML에서 의 값을 구문 분석하여 InputFilter.LengthFilter()적용하는 데 사용 합니다.

참조 : TextView.java # L1564


이것은 길이 필터가 다른 필터와 함께 존재할 수 있도록하는 사용자 정의 EditText 클래스입니다. Tim Gallagher의 답변 (아래) 덕분에

import android.content.Context;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.widget.EditText;


public class EditTextMultiFiltering extends EditText{

    public EditTextMultiFiltering(Context context) {
        super(context);
    }

    public EditTextMultiFiltering(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextMultiFiltering(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setMaxLength(int length) {
        InputFilter curFilters[];
        InputFilter.LengthFilter lengthFilter;
        int idx;

        lengthFilter = new InputFilter.LengthFilter(length);

        curFilters = this.getFilters();
        if (curFilters != null) {
            for (idx = 0; idx < curFilters.length; idx++) {
                if (curFilters[idx] instanceof InputFilter.LengthFilter) {
                    curFilters[idx] = lengthFilter;
                    return;
                }
            }

            // since the length filter was not part of the list, but
            // there are filters, then add the length filter
            InputFilter newFilters[] = new InputFilter[curFilters.length + 1];
            System.arraycopy(curFilters, 0, newFilters, 0, curFilters.length);
            newFilters[curFilters.length] = lengthFilter;
            this.setFilters(newFilters);
        } else {
            this.setFilters(new InputFilter[] { lengthFilter });
        }
    }
}

xml에서 간단한 방법 :

android:maxLength="4"

xml 편집 텍스트에서 4 문자를 설정해야한다면 이것을 사용하십시오.

<EditText
    android:id="@+id/edtUserCode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLength="4"
    android:hint="Enter user code" />

이것은 잘 작동합니다 ...

android:maxLength="10"

10문자 만 허용합니다 .


좋은 솔루션을 많이 보았지만 다음을 포함하여보다 완전하고 사용자 친화적 인 솔루션이라고 생각하는 것을 제공하고 싶습니다.

1, Limit length.
2, If input more, give a callback to trigger your toast.
3, Cursor can be at middle or tail.
4, User can input by paste a string.
5, Always discard overflow input and keep origin.

public class LimitTextWatcher implements TextWatcher {

    public interface IF_callback{
        void callback(int left);
    }

    public IF_callback if_callback;

    EditText editText;
    int maxLength;

    int cursorPositionLast;
    String textLast;
    boolean bypass;

    public LimitTextWatcher(EditText editText, int maxLength, IF_callback if_callback) {

        this.editText = editText;
        this.maxLength = maxLength;
        this.if_callback = if_callback;
    }

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

        if (bypass) {

            bypass = false;

        } else {

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(s);
            textLast = stringBuilder.toString();

            this.cursorPositionLast = editText.getSelectionStart();
        }
    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().length() > maxLength) {

            int left = maxLength - s.toString().length();

            bypass = true;
            s.clear();

            bypass = true;
            s.append(textLast);

            editText.setSelection(this.cursorPositionLast);

            if (if_callback != null) {
                if_callback.callback(left);
            }
        }

    }

}


edit_text.addTextChangedListener(new LimitTextWatcher(edit_text, MAX_LENGTH, new LimitTextWatcher.IF_callback() {
    @Override
    public void callback(int left) {
        if(left <= 0) {
            Toast.makeText(MainActivity.this, "input is full", Toast.LENGTH_SHORT).show();
        }
    }
}));

What I failed to do is, if user highlight a part of the current input and try to paste an very long string, I don't know how to restore the highlight.

Such as, max length is set to 10, user inputed '12345678', and mark '345' as highlight, and try to paste a string of '0000' which will exceed limitation.

When I try to use edit_text.setSelection(start=2, end=4) to restore origin status, the result is, it just insert 2 space as '12 345 678', not the origin highlight. I'd like someone solve that.


android:maxLength="10"EditText에서 사용할 수 있습니다 . (최대 10 자 제한)


프로그래밍 방식 으로 Java에 대해 시도하십시오 .

myEditText(new InputFilter[] {new InputFilter.LengthFilter(CUSTOM_MAX_LEN)});

참고 URL : https://stackoverflow.com/questions/3285412/limit-text-length-of-edittext-in-android

반응형