how to set only numeric value for EditTextPreference in android?

前端 未结 6 2076
轻奢々
轻奢々 2020-12-15 15:15

how to set only numeric value for EditTextPreference in android. I want a user to enter a port number not sure how can I put the limitation there

I am using this co

6条回答
  •  -上瘾入骨i
    2020-12-15 15:56

    I use an androidX library because this library has a possibility to customize an input type of EditTextPreference dialog. AndroidX is a major improvement to the original Android Support Library so is suggested that everybody use this library. You can read more about AndroidX here.

    Here is my code where I use EditTextPreference inside of onCreatePreference method:

     @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.preference, rootKey);
    
    
    
    androidx.preference.EditTextPreference editTextPreference = getPreferenceManager().findPreference("use_key_from_editTextPreference_in_xml_file");
    editTextPreference.setOnBindEditTextListener(new androidx.preference.EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
    
                }
            });
    
           }
    

    After you use this code and click on editTextPreference, the dialog will pop up and your keyboard input type will be only numeric.

提交回复
热议问题