1

你好!

我有一个实现 TimePicker 的 DialogFragment 类。DialogFragment 与 PreferenceFragment 类绑定,该类又由 Activity 持有。在我的首选项中,我还有 2 个可点击的首选项,“设置”和“退出”。我只想在按下“设置”按钮时保存从 TimePicker 选取的值,如果按下“退出”,我不想保存来自 TimePicker 的值,而是使用上次设置的值“设置” "被按下。基本上,我想要与默认 android 闹钟相同的行为。

如何才能做到这一点?我不知道如何从 PreferenceFragment 类“取消保存”我在 DialogFragment 中获得的值(这是我初始化“设置”和“退出”按钮的类)

我也发布了代码,但我认为你不必通读它们来回答问题。

PreferenceFragment 类实现可点击的首选项:

public class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

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

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

        Preference setbutton = (Preference)getPreferenceManager().findPreference("set");      
        if(setbutton != null) 
        {
             setbutton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                         @Override
                         public boolean onPreferenceClick(Preference arg0) {
                             return true;
                         }
                     });     
        }
        Preference exitbutton = (Preference)getPreferenceManager().findPreference("close");      
        if(exitbutton != null) 
        {
             exitbutton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                         @Override
                         public boolean onPreferenceClick(Preference arg0) {
                             return true;
                         }
                     });     
        }
    }

DialogFragment 实现 TimePicker 对话框:

public class TimePreference extends DialogPreference {
    private int lastHour=0;
    private int lastMinute=0;
    private TimePicker picker=null;
    private Calendar calendar = Calendar.getInstance();

    public static int getHour(String time) {
        String[] pieces=time.split(":");

        return(Integer.parseInt(pieces[0]));
    }

    public static int getMinute(String time) {
        String[] pieces=time.split(":");

        return(Integer.parseInt(pieces[1]));
    }

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

    public TimePreference(Context ctxt, AttributeSet attrs) {
        this(ctxt, attrs, 0);
    }

    public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {
        super(ctxt, attrs, defStyle);

        setPositiveButtonText("Set");
        setNegativeButtonText("Cancel");
    }

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

        return(picker);
    }

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

        picker.setCurrentHour(lastHour);
        picker.setCurrentMinute(lastMinute);
    }

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

        if (positiveResult) {
            lastHour=picker.getCurrentHour();
            lastMinute=picker.getCurrentMinute();

            String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);

            if (callChangeListener(time)) {
                persistString(time);

            }
            setSummary(time);
        }
    }

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

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        String time=null;

        if (restoreValue) {
            if (defaultValue==null) {
                time=getPersistedString("00:00");
            }
            else {
                time=getPersistedString(defaultValue.toString());
            }
        }
        else {
            time=defaultValue.toString();
        }

        lastHour=getHour(time);
        lastMinute=getMinute(time);

        setSummary(time);
    }
}

非常感谢!

4

0 回答 0