Android:在PreferenceActivity中使用来自TimePickerDialog的数据

时间:2013-07-01 11:18:12

标签: android dialog settings sharedpreferences preference

我正在尝试创建一个设置时间的设置。然后我想把这个时间用在另一个片段中。我不知道(也找不到)如何从TimePickerDialog获取数据并在片段/活动中使用它。我知道我需要使用SharedPreferences,但我很困惑(特别是在阅读之后:http://developer.android.com/guide/topics/ui/settings.html !!)。

我按照TimePicker in PreferenceScreen创建了对话框

时间存储为长时间,因此更容易与日历一起使用。对话框工作正常 - 时间设置和恢复正确。

代码:

Settings.java

public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener {

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences); 
        }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
        // TODO Auto-generated method stub

    }

这是自定义对话框,可以将其放在首选项屏幕

public class TPP2 extends DialogPreference {    

    private Calendar calendar;
        private TimePicker picker = null;


        public TPP2(Context ctxt) {
            this(ctxt, null);
        }

        public TPP2(Context ctxt, AttributeSet attrs) {
            super(ctxt, attrs);

            setPositiveButtonText("Set");
            setNegativeButtonText("Cancel");
            calendar = new GregorianCalendar();
        }

        @Override
        protected View onCreateDialogView() {
            picker = new TimePicker(getContext());

            return (picker);
        }

        @Override
        protected void onBindDialogView(View v) {
            super.onBindDialogView(v);

            picker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
            picker.setCurrentMinute(calendar.get(Calendar.MINUTE));

        }

        @Override
        protected void onDialogClosed(boolean positiveResult) {
            super.onDialogClosed(positiveResult);

            if (positiveResult) {
                calendar.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
                calendar.set(Calendar.MINUTE, picker.getCurrentMinute());

                persistLong (calendar.getTimeInMillis()); //

                setSummary(getSummary());
                if (callChangeListener(calendar.getTimeInMillis())) {
                    persistLong(calendar.getTimeInMillis());
                    notifyChanged();
                }
            }
        }

        @Override
        protected Object onGetDefaultValue(TypedArray a, int index) {
            return (a.getString(index));
        }

        @Override
        protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {

            if (restoreValue) {
                if (defaultValue == null) {
                    calendar.setTimeInMillis(getPersistedLong(System
                            .currentTimeMillis()));

                } else {
                    calendar.setTimeInMillis(Long
                            .parseLong(getPersistedString((String) defaultValue)));

                }
            } else {
                if (defaultValue == null) {
                    calendar.setTimeInMillis(System.currentTimeMillis());

                } else {
                    calendar.setTimeInMillis(Long.parseLong((String) defaultValue));

                }
            }
            setSummary(getSummary());
        }

        @Override
        public CharSequence getSummary() {
            if (calendar == null) {
                picker.setCurrentHour(8);
                picker.setCurrentMinute(0);
                return null;
            }
            return DateFormat.getTimeFormat(getContext()).format(
                    new Date(calendar.getTimeInMillis()));
        }
    }

偏好代码:

 <PreferenceCategory android:title="Title" >
        <com.xxx.TPP2
            android:key="rdTime"
            android:summary="Summary"
            android:title="Set Time" />
    </PreferenceCategory>

谢谢

0 个答案:

没有答案