Hurro!
我正在尝试编写我的第一个Android应用程序,而且我遇到了一堵砖墙。
我希望允许用户为每日时间表设置“开始”和“停止”时间。我已经成功实现了TimePickerDialog和它旁边的TextView,但我不能为我的LIFE弄清楚如何将用户选择的时间保存到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,但这不起作用,因为我需要将时间存储为long,而不是String。
我已经走到了尽头,我真的很感激一些帮助!!
答案 0 :(得分:2)
以下方法
// 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, "");
答案 1 :(得分:1)
你必须这样做
private TimePickerDialog.OnTimeSetListener mTimeSetListenerStart =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHourStart = hourOfDay;
mMinuteStart = minute;
updateDisplayStart();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
mUserSettings.edit().putLong("starttime", cal.getTimeInMillis()).commit();
}
};
答案 2 :(得分:0)
SharedPreferences
支持long
。你可以做点什么
SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
prefs.edit().putLong("longKey", longValue).commit();
存储值。要检索它,你可以
SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
long longValue = prefs.getLong("longKey", 0l);
答案 3 :(得分:0)
这一个用于编辑SharedPreference文件的代码: -
获取值SharedPreference文件的这一个代码: -
X ==获取信息