如何在PreferenceFragment中使用DatePickerFragment来获取Date

时间:2014-11-24 08:08:34

标签: android android-fragments preferenceactivity preferencefragment

我想在PreferenceFragment中使用DatePickerFragment作为出生日期。我们可以像使用EditTextPreference或我们必须使用的任何其他方式一样使用它。我知道DatePickerFragemnt和PreferenceFragment的基本功能如何。

任何帮助都是适当的。

被修改

首选项的

.xml文件

    

    <PreferenceCategory android:title="@string/pref_profile_setting" >
        <EditTextPreference
                android:title="@string/pref_user_name"
                android:summary="@string/pref_user_name_summary"
                android:key="prefName"/>

        <com.mypkg.DatePickerFragment
            android:title="@string/pref_gender"
                android:summary="@string/pref_postcode_summary"
                android:key="prefDob"
                />
        <ListPreference
                android:title="@string/pref_gender"
                android:summary="@string/pref_postcode_summary"
                android:key="prefGender"
                android:entries="@array/gender"
                android:entryValues="@array/gender_type"/>

    </PreferenceCategory>
</PreferenceScreen>
  

扩展PreferenceFragment的类

public class MyFragment extends PreferenceFragment implements OnPreferenceChangeListener{


    private EditTextPreference etNamePref;
    private ListPreference lvGenderPref;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_settings);

    }

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle paramBundle) {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        initView();

        return view;
    }


    private void initView() {
         etNamePref = (EditTextPreference)findPreference(ConstantsUtil.PREF_NAME);
         etNamePref.setOnPreferenceChangeListener(this);

    }


    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        String key = preference.getKey();
        if(key.equalsIgnoreCase(ConstantsUtil.PREF_NAME)){
            etNamePref.setSummary(newValue.toString());
        }
        return false;
    }
}

2 个答案:

答案 0 :(得分:1)

您必须为此创建自己的自定义DialogPreference,如下所示

public class DatePickerPreference extends DialogPreference {


    public DatePickerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DatePickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        // TODO :: Set the initial value of the Preference.
    }

    @Override
    protected View onCreateDialogView() {
        return new DatePicker(getContext());
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        // TODO :: Save data to the SharedPreferences.
    }

}

答案 1 :(得分:1)

在.xml文件中添加首选项标记

<PreferenceCategory android:title="@string/pref_profile_setting" >
        <EditTextPreference
                android:title="@string/pref_user_name"
                android:summary="@string/pref_user_name_summary"
                android:key="prefName"/>

<Preference android:title="@string/pref_select_dob"
             android:key="prefDob" >
        <ListPreference
                android:title="@string/pref_gender"
                android:summary="@string/pref_postcode_summary"
                android:key="prefGender"
                android:entries="@array/gender"
                android:entryValues="@array/gender_type"/>

    </PreferenceCategory>
</PreferenceScreen>

在.java类中实现 OnPreferenceClickListener 及以下代码

Preference preference = (Preference)findPreference("prefDob");
        preference.setOnPreferenceClickListener(this);

在onClick内部调用DatePickerFragment,如下所示

@Override
    public boolean onPreferenceClick(Preference preference) {
        String key = preference.getKey();
        if(key.equalsIgnoreCase("prefDob")){
            showDatePicker();
        }
        return false;
    }

    protected void showDatePicker() {

        DatePickerFragment date = new DatePickerFragment();

         /**
         * Set Up Current Date Into dialog
         */

        Calendar calender = Calendar.getInstance();
        Bundle args = new Bundle();
        args.putInt("year", calender.get(Calendar.YEAR));
        args.putInt("month", calender.get(Calendar.MONTH));
        args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
        date.setArguments(args);

         /**
         * Set Call back to capture selected date
         */

        date.setCallBack(ondate);
        date.show(getFragmentManager(), "Date Picker");

    }
    OnDateSetListener ondate = new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            String dob = String.valueOf(dayOfMonth) + "/" + String.valueOf(monthOfYear)+ "/" + String.valueOf(year);

        }
    };

完成!