EditTextPreference - only numeric value inputType - isn't working

前端 未结 8 622
迷失自我
迷失自我 2021-01-18 10:16


        
8条回答
  •  死守一世寂寞
    2021-01-18 10:38

    The Customize your settings section in the developer guide for settings recommends that you set the input type programmatically by using an OnBindEditTextListener as follows:

    public class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.settings_screen, rootKey);
    
            EditTextPreference weeklyGoalPref = findPreference("weekly_goal");
            if (weeklyGoalPref != null) {
                weeklyGoalPref.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
                    @Override
                    public void onBindEditText(@NonNull EditText editText) {
                        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                    }
                });
            }
        }
    }
    

    I tried defining the input type as "number" in xml and still got the normal keyboard with letters. This approach worked for me.

提交回复
热议问题