我的设置屏幕上有时间选择器对话框。当用户点击时间选择器对话框出现并且用户选择时间时。但是当用户再次点击时间选择器时,时间选择器对话框显示当前时间而不是选定时间。 这是我到目前为止所做的。
mreminder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
final int[] hour = {mcurrentTime.get(Calendar.HOUR_OF_DAY)};
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(SettingsActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
int hour = selectedHour;
int minutes = selectedMinute;
String timeSet = " ";
if (hour > 12) {
hour -= 12;
timeSet = "PM";
} else if (hour == 0) {
hour += 12;
timeSet = "AM";
} else if (hour == 12)
timeSet = "PM";
else
timeSet = "AM";
String min = "";
if (minutes < 10)
min = "0" + minutes;
else
min = String.valueOf(minutes);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hour).append(':')
.append(min).append(" ").append(timeSet).toString();
mreminder.setText(aTime);
pref.savePref(REMINDER, aTime);
}
}, hour[0], minute, false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
有人可以告诉我如何在下次用户点击时将时间戳设置为所选时间。
答案 0 :(得分:0)
将所选时间存储在sharedPreferences中,并在onClick
事件中检查sharedPreference
是否有某些内容。如果它有东西然后从那里加载数据
你可以这样做:
int hours,mins;//global variables;
mreminder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int h = Integer.parseInt(sharedPreference.getString("HOUR","0));
int m = Integer.parseInt(sharedPreference.getString("MIN","0"));//"0" is the default value if nothing is stored;
if(h>0){
hours = h;
mins = m;
}else{
Calendar mcurrentTime = Calendar.getInstance();
hours = mcurrentTime.get(Calendar.HOUR_OF_DAY);
mins = mcurrentTime.get(Calendar.MINUTE);
}
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(SettingsActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
int hour = selectedHour;
int minutes = selectedMinute;
String timeSet = " ";
if (hour > 12) {
hour -= 12;
timeSet = "PM";
} else if (hour == 0) {
hour += 12;
timeSet = "AM";
} else if (hour == 12)
timeSet = "PM";
else
timeSet = "AM";
String min = "";
if (minutes < 10)
min = "0" + minutes;
else
min = String.valueOf(minutes);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hour).append(':')
.append(min).append(" ").append(timeSet).toString();
mreminder.setText(aTime);
pref.savePref(REMINDER, aTime);
}
}, hours, mins, false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
这将解决您的问题。