Android control EditTextPreference can only enter numbers

The PrefrenceAcitivity application was created in the Android project. For the control EditTextPreference, I want to limit the input of numbers only. I searched for a long time. First of all,

Set properties in the layout

android:inputType="number"
android:digits="0123456789"

This is completely unacceptable, there is no such property at all. Code hints don't pop up at all. In the Design view, it cannot be added at all in the Declared Attributes on the right.

 

In fact, the implementation is quite simple as follows:

The first step is to get the control through the key.

The second step is to set OnBindEditTextListener.

EditTextPreference numberPreference = findPreference("number");

if (numberPreference != null) {
    numberPreference.setOnBindEditTextListener(
            new EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                }
            });
}

 

Guess you like

Origin blog.csdn.net/wangmy1988/article/details/114587866