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

发布于 2024-11-08 02:39:09 字数 74 浏览 0 评论 0原文

如何设置 Android EditText 输入的最大字符数?我看到了 setMaxLines、setMaxEMS,但没有看到字符数。

How do you set the max number of characters for an Android EditText input? I see setMaxLines, setMaxEMS, but nothing for the number of characters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

ぶ宁プ宁ぶ 2024-11-15 02:39:09

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

android:maxLength="140"

In XML you can add this line to the EditText View where 140 is the maximum number of characters:

android:maxLength="140"
梦中的蝴蝶 2024-11-15 02:39:09

动态:

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

通过 xml:

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

Dynamically:

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

Via xml:

<EditText
    android:maxLength="@integer/max_edittext_length"
天生の放荡 2024-11-15 02:39:09

您可以使用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);

You can use a InputFilter, that's the way:

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);
阳光下的泡沫是彩色的 2024-11-15 02:39:09

我总是这样设置最大值:

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

I always set the max like this:

    <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"
        />
寻找我们的幸福 2024-11-15 02:39:09

它对我有用。

    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

        }
    });

It is working for me.

    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

        }
    });
明明#如月 2024-11-15 02:39:09
   <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"/>

输入类型必须设置“textFilter”

        android:inputType="textFilter"
   <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 has to set "textFilter"

        android:inputType="textFilter"
爱她像谁 2024-11-15 02:39:09

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

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

use this function anywhere easily:

 public static void setEditTextMaxLength(EditText editText, int length) {
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(length);
    editText.setFilters(FilterArray);
  }
圈圈圆圆圈圈 2024-11-15 02:39:09

科特林方式

editText.filters = arrayOf(InputFilter.LengthFilter(maxLength))

Kotlin way

editText.filters = arrayOf(InputFilter.LengthFilter(maxLength))
过度放纵 2024-11-15 02:39:09

这种方式对我有用,这是我发现的最好的方式。
最大长度为 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) {
    }
});

It worked for me this way, it's the best I've found.
It is for a max length of 200 characters

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) {
    }
});
星軌x 2024-11-15 02:39:09

显然,您可以在 xml 或 InputFilter.LengthFilter 中使用 maxLength,如上面所回答。但对我来说,在某些情况下,这还不够。我创建了一个类来更灵活地设置 EditText: https://github.com/devapro/NumberWatcher
它仅适用于数字输入,但您可以更改任何类型。

Obviously you can use maxLength in xml or InputFilter.LengthFilter as answered above. But for me in some cases, it was not enough. I created a class for more flexible settings of EditText: https://github.com/devapro/NumberWatcher
It is implementation only for numbers input, but you can change it for any of the types.

莫多说 2024-11-15 02:39:09

它不适用于带有 maxLenght 的 XML
我用的是这段代码,可以限制字符数

String editorName = mEditorNameNd.getText().toString().substring(0, Math.min(mEditorNameNd.length(), 15));

it doesn't work from XML with maxLenght
I used this code, you can limit the number of characters

String editorName = mEditorNameNd.getText().toString().substring(0, Math.min(mEditorNameNd.length(), 15));

享受孤独 2024-11-15 02:39:09

您可以向提交或更改事件添加验证挂钩。

if (EditText.length() <=10){
            Toast.makeText(MainAcitvity.this, "Enter Valid Number", Toast.LENGTH_SHORT).show();
            contact.setError("Enter Valid");
            return false;
        }

You could add a validation hook to a submit or change event.

if (EditText.length() <=10){
            Toast.makeText(MainAcitvity.this, "Enter Valid Number", Toast.LENGTH_SHORT).show();
            contact.setError("Enter Valid");
            return false;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文