【问题标题】:Is it possible to restrict a EditTextPreference to numbers?是否可以将 EditTextPreference 限制为数字?
【发布时间】:2020-12-19 19:32:42
【问题描述】:

我的应用设置中有一个EditTextPreference,我只想接受正十进制数:

<EditTextPreference
    android:key="annual_vacation_setting"
    android:title="@string/enter_annual_vacation"
    android:defaultValue="@string/default_annual_vacation"
    android:inputType="numberDecimal"
    android:selectAllOnFocus="true" />

不幸的是,inputType 属性似乎没有任何作用。用户可以输入任意字符串。

【问题讨论】:

标签: android sharedpreferences android-preferences


【解决方案1】:

解决方案 1

您需要在onBinding 之后将inputType 设置为其EditText

editTextPrefView.setOnBindEditTextListener { editText ->
    editText.inputType = InputType.TYPE_CLASS_NUMBER
}

解决方案 2

使用这个FIX library。它会让你直接设置 XML

中的android:inputType=numberDecimalEditTextPreference

【讨论】:

    猜你喜欢
    • 2020-08-06
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    相关资源
    最近更新 更多