How to use DatePickerDialog as a Preference

后端 未结 6 900
梦谈多话
梦谈多话 2021-02-18 20:53

I have preferences working and I am using a combination of CheckBoxPreference and EditTextPreference. I would like to replace one of them with a DatePickerDialog.

When m

6条回答
  •  [愿得一人]
    2021-02-18 20:57

    Just a simple way to use TimePickerFragment in the settings, it doesn't really answer your question, but it can help some guys.
    Please read this before: https://developer.android.com/guide/topics/ui/settings
    From "Overview" to "Preference... attributes" of course... Oo

    controller.fragments

    public class TimePickerFragment extends DialogFragment {
    
        private TimePickerDialog.OnTimeSetListener onTimeSetListener;
        private int hours;
        private int minutes;
    
        TimePickerFragment(TimePickerDialog.OnTimeSetListener onTimeSetListener, int hours, int minutes) {
            this.onTimeSetListener = onTimeSetListener;
            this.hours = hours;
            this.minutes = minutes;
        }
    
        @NonNull
        @Override
        public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
            return new TimePickerDialog(getActivity(), R.style.dateTimePicker,
                    onTimeSetListener, hours, minutes, DateFormat.is24HourFormat(getActivity()));
        }
    }
    

    res.values.style

    
    

    res.xml.root_preferences.xml create the xml folder in res & the file root_preferences of course Oo

    
        
    
            
    
        
    
    

    controller.activities.SettingsActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.settings, new SettingsFragment())
                .commit();
    }
    

    controller.fragments.SettingsFragment

    public class SettingsFragment extends PreferenceFragmentCompat implements TimePickerDialog.OnTimeSetListener {
    
    
             private Preference setTime;
    
             public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
                setPreferencesFromResource(R.xml.root_preferences, rootKey);
                getPreference();
                configListener();
            }
    
            private void getPreference() {
                setTime = findPreference("set_time");
            }
    
            private void configListener() {
                if (setTime != null){
                    setTime.setOnPreferenceClickListener(preference -> {
                        showTimeDialog(preference);
                        return true;
                    });
                }
            }
    
            private void showTimeDialog(Preference preference) {
                String value = preference.getSharedPreferences().getString("set_time", "12:00");
                String[] time = value.split(":");
                int hours = Integer.parseInt(time[0]);
                int minutes = Integer.parseInt(time[1]);
                if (getFragmentManager() != null) {
                new TimePickerFragment(this, hours, minutes)
                    .show(getFragmentManager(), getString(R.string.tag_time_picker));
                }
            }
    
            @Override
            public void onTimeSet(TimePicker timePicker, int h, int m) {
                String time = format(Locale.getDefault(),"%02d", h) + ":" + format(Locale.getDefault(), "%02d", m);
                SharedPreferences sharedPreferences =
                        PreferenceManager.getDefaultSharedPreferences(context);
                sharedPreferences.edit().putString("set_time", time).apply();
                // if you use setOnPreferenceChangeListener on it, use setTime.callChangeListener(time);
            }
    }
    

    I didn't document anything because after reading the guide, you should all understand ^^

提交回复
热议问题