【问题标题】:AppCompatEditText android:digits not working?AppCompatEditText android:数字不起作用?
【发布时间】:2016-05-28 19:15:27
【问题描述】:

AppCompatEdittext 的 android:digits 属性无法正常工作。

我的 XML

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:digits="12345" />

但是当我按数字 6,7,8,9,0 之前的字符重复了? 不知道这是怎么回事?

【问题讨论】:

  • 你想让用户只输入 1 到 5 对吗??
  • 添加android:inputType="number"
  • 我只想允许 1 到 5 并且 onTextChangeListner(...) android 也可以在一行中实现这一点,因此我可以使用此功能
  • 我尝试了所有其他解决方案并最终实现了它。看我的回答here

标签: android android-appcompat


【解决方案1】:

添加inputType="number":

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:digits="12345"
    android:inputType="number"/>

【讨论】:

  • 这在我的情况下不起作用,我尝试了所有其他解决方案并最终实现了它。看我的回答here
  • 不是一个相关的答案。如果某人必须只允许使用字母数字字符和 + 和 - 符号怎么办?
【解决方案2】:

首先你应该添加android:inputType="number"

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="12345"
android:inputType="number"/>

其次

 private final TextWatcher  mTextEditorWatcher = new TextWatcher() {

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

                }

                public void onTextChanged(CharSequence s, int start, int before, int count)
                {
                     // Logic in here
                .setKeyListener(DigitsKeyListener.getInstance("12345"));
                }

                public void afterTextChanged(Editable s)
                {

                }
        };

【讨论】:

    猜你喜欢
    • 2021-02-19
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2014-09-18
    • 1970-01-01
    • 2011-08-29
    相关资源
    最近更新 更多