1

我想在 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;
    }
}
4

2 回答 2

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);

        }
    };

完毕 !

于 2014-11-25T06:14:06.460 回答
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.
    }

}
于 2014-11-24T09:26:05.777 回答