EditTextPreference - only numeric value inputType - isn't working

前端 未结 8 539
迷失自我
迷失自我 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.

    0 讨论(0)
  • 2021-01-18 10:43

    Thought I'd add my solution for Android-TV in case someone gets here and needs it. android_su's reply put me in the right direction, unfortunately, the LeanbackEditTextPreferenceDialogFragmentCompat doesn't support OnPreferenceDisplayDialogCallback. Instead I had to:

    override onCreateView and implement a new newInstance function for LeanbackEditTextPreferenceDialogFragmentCompat using inheritance:

    public static class TvPreferencesDialog extends LeanbackEditTextPreferenceDialogFragmentCompat {
    
        public static TvPreferencesDialog newInstance(String key) {
            final Bundle args = new Bundle(1);
            args.putString(ARG_KEY, key);
    
            final TvPreferencesDialog fragment = new TvPreferencesDialog();
            fragment.setArguments(args);
    
            return fragment;
        }
    
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View root = super.onCreateView(inflater, container, savedInstanceState);
            final String key = getArguments().getString(ARG_KEY);
            EditText editTextView = root.findViewById(android.R.id.edit);
    
            if (key.equalsIgnoreCase("some pref key 1")) {
                editTextView.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
            } else if (key.equalsIgnoreCase("some pref key 2")) {
                editTextView.setInputType(InputType.TYPE_CLASS_NUMBER);
                editTextView.addTextChangedListener(new MyTextWatcher());
            }
    
            return root;
        }    
    }
    

    override onPreferenceDisplayDialog inside my LeanbackSettingsFragmentCompat:

    @Override
    public boolean onPreferenceDisplayDialog(@NonNull PreferenceFragmentCompat caller, Preference pref) {
        if (caller == null) {
            throw new IllegalArgumentException("Cannot display dialog for preference " + pref + ", Caller must not be null!");
        }
        final Fragment f;
        if (pref instanceof EditTextPreference) {
            f = TvPreferencesDialog.newInstance(pref.getKey());
            f.setTargetFragment(caller, 0);
            startPreferenceFragment(f);
            return true;
        } else {
            return super.onPreferenceDisplayDialog(caller, pref);
        }
    }
    
    0 讨论(0)
提交回复
热议问题