gpt4 book ai didi

java - SharedPreferences 和 TimePickerDialog

转载 作者:行者123 更新时间:2023-12-02 00:26:59 27 4
gpt4 key购买 nike

哈罗!
我正在尝试编写我的第一个 Android 应用程序,但遇到了困难。

我想允许用户为每日计划设置“开始”和“停止”时间。我已经成功实现了 TimePickerDialog 及其旁边的 TextView,但我一生都无法弄清楚如何将用户选择的时间保存到 SharedPreferences 字符串中。

代码如下(为了清晰起见,进行了精简):

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup);
// Retrieve the shared preferences
mUserSettings = getSharedPreferences(USER_PREFERENCES, MODE_PRIVATE);

// capture our View elements
mTimeDisplayStart = (TextView) findViewById(R.id.TextView_ScheduleStart_Info);
mPickTimeStart = (Button) findViewById(R.id.Button_ScheduleStart);

// add a click listener to the button
mPickTimeStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_START);
}
});

// get the current time
final Calendar c = Calendar.getInstance();
mHourStart = c.get(Calendar.HOUR_OF_DAY);
mMinuteStart = c.get(Calendar.MINUTE);

// display the current date
updateDisplayStart();
}

// updates the time we display in the TextView
private void updateDisplayStart() {
mTimeDisplayStart.setText(
new StringBuilder()
.append(padStart(mHourStart)).append(":")
.append(padStart(mMinuteStart)));
}

private static String padStart(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}

// the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListenerStart =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHourStart = hourOfDay;
mMinuteStart = minute;
updateDisplayStart();
}
};

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_START:
return new TimePickerDialog(this,
mTimeSetListenerStart, mHourStart, mMinuteStart, false);
}
return null;
}

我认为应该在更新 TextView 的同时保存时间...但我不知道该怎么做 -.-

我现在已经花了 5 或 6 个小时来寻找答案,但我确实找不到答案。我已经尝试过 SharedPreferences.Editor,但这不起作用,因为我需要将时间存储为长整数,而不是字符串。
我已经束手无策了,非常感谢您的帮助!!

最佳答案

通过以下方法

    // updates the time we display in the TextView
private void updateDisplayStart() {
mTimeDisplayStart.setText(
new StringBuilder()
.append(padStart(mHourStart)).append(":")
.append(padStart(mMinuteStart)));
});

SharedPreferences settingsActivity = getSharedPreferences("todolist1",
Activity.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settingsActivity.edit();

prefEditor.putString("DateToDisplay",new StringBuilder()
.append(padStart(mHourStart)).append(":")
.append(padStart(mMinuteStart)); // updates the time we display in the TextView



prefEditor.commit();
}

在此之后,无论您想要从共享首选项中检索数据,如下所示

SharedPreferences settingsActivity = getSharedPreferences("todolist1",
Activity.MODE_PRIVATE);

if (settingsActivity.contains(DateToDisplay)) {
String saveddate = settingsActivity
.getString(DateToDisplay, "");

关于java - SharedPreferences 和 TimePickerDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798563/

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