是否可以将EditTextPreference限制为数字?

时间:2020-08-31 16:46:36

标签: android sharedpreferences android-preferences

我的应用程序设置中有一个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属性似乎没有任何作用。用户可以输入任意字符串。

1 个答案:

答案 0 :(得分:1)

解决方案1 ​​

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

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

解决方案2

使用此FIX library。它会让你直接设置 在 XML

中从android:inputType=numberDecimalEditTextPreference
相关问题