How do I programmatically add EditTextPreferences to my PreferenceFragment?

前端 未结 3 2127
北荒
北荒 2021-02-15 14:36

I am new to Android, so I need a little guidance on how to programmatically add EditTextPreference objects to my PreferenceFragment

3条回答
  •  清酒与你
    2021-02-15 15:06

    Here is the code to create a PreferenceFragment programmatically:

    public class MyPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(getActivity());
            setPreferenceScreen(screen);
    
            EditTextPreference preference = new EditTextPreference(screen.getContext());
            preference.setKey("EditTextPreference");
            preference.setTitle("Edit Text Preference");
            preference.setSummary("Click the preference to edit text.");
            screen.addPreference(preference);
        }
    }
    

提交回复
热议问题