EditTextAndroid でのテキストの長さを制限する最良の方法は何ですか?
これをxml経由で行う方法はありますか?
EditTextAndroid でのテキストの長さを制限する最良の方法は何ですか?
これをxml経由で行う方法はありますか?
回答:
maxLengthプロパティがまだ動作します。
                    android:maxLength同等であることに注意してください。そのInputFilter.LengthFilterため、プログラムでフィルターを変更すると、XMLフィルターも変更されます。
                    setFiltersが機能しなくなることに注意してandroid:maxLengthください。つまり、プログラムでフィルタを設定する場合は、すべてプログラムで設定する必要があります。
                    入力フィルターを使用して、テキストビューの最大長を制限します。
TextView editEntryView = new TextView(...);
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(8);
editEntryView.setFilters(filterArray);
              InputFilterます。android:maxlengthxmlファイルでオーバーライドするため、LengthFilterこの方法で追加する必要があります。
                    EditText editText = new EditText(this);
int maxLength = 3;    
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
              TextWatcher。
                    すでにカスタム入力フィルタを使用している人々への注記も最大長さを制限したいです。
コードで入力フィルターを割り当てると、以前に設定されたすべての入力フィルターがクリアされandroid:maxLengthます。カスタム入力フィルターを使用して、パスワードフィールドで許可されていない一部の文字を使用しないようにしようとしたときに、このことがわかりました。setFiltersでそのフィルターを設定した後、maxLengthは観察されなくなりました。解決策は、プログラムでmaxLengthと私のカスタムフィルターを一緒に設定することでした。このようなもの:
myEditText.setFilters(new InputFilter[] {
        new PasswordCharFilter(), new InputFilter.LengthFilter(20)
});
              私はこの問題を抱えていて、すでに設定されているフィルターを失うことなく、プログラムでこれを行う十分に説明された方法がないと考えています。
XMLでの長さの設定:
受け入れられた回答が正しく述べているように、今後変更しないEditTextに固定長を定義する場合は、EditText XMLで定義するだけです。
android:maxLength="10" 
プログラムによる長さの設定
プログラムで長さを設定するには、を使用して長さを設定する必要がありますInputFilter。ただし、新しいInputFilterを作成して、EditText XMLまたはプログラムで追加した可能性のある他のすべての定義済みフィルター(maxLines、inputTypeなど)が失われます。
これは間違っています:
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
以前に追加したフィルターが失われないようにするには、それらのフィルターを取得し、新しいフィルター(この場合は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(maxLength); 
editText.setFilters(newFilters);
ただし、Kotlinを使用すると誰にとっても簡単になり、既存のフィルターにフィルターを追加する必要がありますが、簡単な方法でそれを実現できます。
editText.filters += InputFilter.LengthFilter(maxLength)
              これを達成する方法を知りたい人のために、ここに私の拡張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
android:maxLength="10"
Java:
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);
コトリン:
editText.filters += InputFilter.LengthFilter(maxLength)
              これを実現するもう1つの方法は、次の定義を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()それを適用するために使用します。
これはカスタムEditTextクラスであり、Lengthフィルターを他のフィルターと共に使用できます。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 });
        }
    }
}
              プログラムでこれをJavaで試してください:
myEditText(new InputFilter[] {new InputFilter.LengthFilter(CUSTOM_MAX_LEN)});
              myEditText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(CUSTOM_MAX_LEN)}); 
                    私は多くの優れたソリューションを見てきましたが、以下を含む、より完全でユーザーフレンドリーなソリューションとして私が考えていることを提供したいと思います。
1、長さを制限します。
2、さらに入力する場合は、トーストをトリガーするコールバックを提供します。
3、カーソルは中央または尾にあります。
4、ユーザーは文字列を貼り付けて入力できます。
5、常にオーバーフロー入力を破棄し、原点を維持します。  
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();
        }
    }
}));
私が失敗したのは、ユーザーが現在の入力の一部をハイライト表示して、非常に長い文字列を貼り付けようとした場合、ハイライト表示を復元する方法がわかりません。
たとえば、最大長が10に設定され、ユーザーが「12345678」を入力し、「345」をハイライトとしてマークし、制限を超える「0000」の文字列を貼り付けようとします。
edit_text.setSelection(start = 2、end = 4)を使用して原点のステータスを復元しようとすると、結果として、原点のハイライトではなく、2つのスペースが「12 345 678」として挿入されます。誰かがそれを解決して欲しい。
コトリン:
edit_text.filters += InputFilter.LengthFilter(10)
ZTE Blade A520奇妙な効果があります。10個を超える記号(15など)を入力すると、EditText最初の10個が表示されますが、他の5個は表示されず、アクセスできません。ただしBackspace、でシンボルを削除すると、最初に右側の5つのシンボルが削除され、次に残りの10個が削除されます。この動作を克服するには、ソリューションを使用します。
android:inputType="textNoSuggestions|textVisiblePassword"
android:maxLength="10"
またはこれ:
android:inputType="textNoSuggestions"
またはこれ、あなたが提案をしたい場合:
private class EditTextWatcher(private val view: EditText) : TextWatcher {
    private var position = 0
    private var oldText = ""
    override fun afterTextChanged(s: Editable?) = Unit
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        oldText = s?.toString() ?: ""
        position = view.selectionStart
    }
    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        val newText = s?.toString() ?: ""
        if (newText.length > 10) {
            with(view) {
                setText(oldText)
                position = if (start > 0 && count > 2) {
                    // Text paste in nonempty field.
                    start
                } else {
                    if (position in 1..10 + 1) {
                        // Symbol paste in the beginning or middle of the field.
                        position - 1
                    } else {
                        if (start > 0) {
                            // Adding symbol to the end of the field.
                            start - 1
                        } else {
                            // Text paste in the empty field.
                            0
                        }
                    }
                }
                setSelection(position)
            }
        }
    }
}
// Usage:
editTextWatcher = EditTextWatcher(view.edit_text)
view.edit_text.addTextChangedListener(editTextWatcher)
              それはxmlで簡単な方法です:
android:maxLength="@{length}"
プログラムで設定するには、次の関数を使用できます
public static void setMaxLengthOfEditText(EditText editText, int length) {
    InputFilter[] filters = editText.getFilters();
    List arrayList = new ArrayList();
    int i2 = 0;
    if (filters != null && filters.length > 0) {
        int length = filters.length;
        int i3 = 0;
        while (i2 < length) {
            Object obj = filters[i2];
            if (obj instanceof LengthFilter) {
                arrayList.add(new LengthFilter(length));
                i3 = 1;
            } else {
                arrayList.add(obj);
            }
            i2++;
        }
        i2 = i3;
    }
    if (i2 == 0) {
        arrayList.add(new LengthFilter(length));
    }
    if (!arrayList.isEmpty()) {
        editText.setFilters((InputFilter[]) arrayList.toArray(new InputFilter[arrayList.size()]));
    }
}