gpt4 book ai didi

android - 保留 DialogPreference 中的多个值

转载 作者:太空狗 更新时间:2023-10-29 14:29:54 24 4
gpt4 key购买 nike

我很难从对话首选项中保留多个值。我有一个显示两个时间选择器的 DialogPreference,我希望每个选择器的时间值都保持不变(如下所示为 startTime 和 endTime)。但是,我的代码仅从我的 DialogPreference 返回 DefValue,而且我不明白为什么这些值没有保留....感谢您的帮助。代码如下:

public class TimePickerPreference extends DialogPreference implements
TimePicker.OnTimeChangedListener {
private String startTime;
private String endTime;


private String defaultValue;
public TimePickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}

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

private void initialize() {
setPersistent(true);
}

@Override
protected View onCreateDialogView() {

LinearLayout layout = new LinearLayout(getContext());
layout.setOrientation(LinearLayout.VERTICAL);
TextView tvStart = new TextView(getContext());
TimePicker tp = new TimePicker(getContext());
TextView tvEnd = new TextView(getContext());
TimePicker tp2 = new TimePicker(getContext());

tvStart.setText("From:");
tvEnd.setText("To:");
tvStart.setTextSize(14);
tvEnd.setTextSize(14);

tp.setOnTimeChangedListener(this);

int h = getHour();
int m = getMinute();
if (h >= 0 && m >= 0) {
tp.setCurrentHour(h);
tp.setCurrentMinute(m);
}

tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
final long starttime = (minute * 60 + hourOfDay * 60 * 60) * 1000;
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
startTime = format.format(starttime);
persistString(startTime);

}
});
tp2.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
final long endtime = (minute * 60 + hourOfDay * 60 * 60) * 1000;
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
endTime = format.format(endtime);
persistString(endTime);
}
});
layout.addView(tvStart);
layout.addView(tp);
layout.addView(tvEnd);
layout.addView(tp2);
return layout;
}

最佳答案

您问题的最佳答案在这里:Concise way of writing new DialogPreference classes?

我认为基于该链接这里有 2 个重要的错误,第一个是 setPersistent(boolean) 应该是 false,第二个是你需要使用 getEditor()DialogPreference 中存储多个 SharedPreferences

关于android - 保留 DialogPreference 中的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7988505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com