您好我正在尝试计算用户7.5的睡眠时间。该应用程序应该建议用户应该在准确的时间在床上。例如,她选择早上7点作为她醒来的时间,应用程序会建议到11:30她应该去睡觉我到目前为止尝试的是:
public class TimepickerActivity extends Activity implements TimePickerDialog.OnTimeSetListener {
Button btn1, btn2;
TimePicker picker;
TextView tv;
private int hour;
private int minute;
static final int TIME_DIALOG_ID = 999;
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView( R.layout.activity_timepicker );
btn1 = (Button) findViewById (R.id.btnShow);
btn2 = (Button) findViewById (R.id.btnSleepingTime);
tv = (TextView) findViewById (R.id.textView1);
picker = (TimePicker) findViewById (R.id.timePicker1);
setCurrentTimeOnView();
addListenerOnButton();
}
// display current time
public void setCurrentTimeOnView() {
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// set current time into textview
tv.setText( new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
// set current time into timepicker
picker.setCurrentHour(hour);
picker.setCurrentMinute(minute);
}
public void addListenerOnButton() {
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this,
timePickerListener, hour, minute,false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR, - selectedHour);
c.add(Calendar.MINUTE, - selectedMinute);
// set current time into timepicker
picker.setCurrentHour(hour + 7);
picker.setCurrentMinute(minute + 30);
// set current time into textview
//tv.setText(new StringBuilder().append(pad(hour))
// .append(":").append(pad(minute)));
tv.setText( picker.getCurrentHour().toString() + ":" + picker.getCurrentMinute().toString() );
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
}
}
当我输入早上7点作为我的清醒时间时,结果是在下午2:30给我。但应该是下午12:30。感谢任何帮助。
答案 0 :(得分:1)
不,这是对的。如果您输入上午7:00 + 7.5小时= 14:30 = 2:30 am。该计划正常运作。
编辑:你几乎做到了:
public class TimepickerActivity extends Activity implements TimePickerDialog.OnTimeSetListener {
private static final int SUB_HOUR = -7;
private static final int SUB_MINUTE = -30;
[...]
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
// Gets current time
Calendar c = Calendar.getInstance();
// Assign hour set in the picker
c.set(Calendar.HOUR, selectedHour)
c.set(Calendar.MINUTE, selectedMinute);
// Have Calendar calculate the substraction of hours and minutes
c.add(Calendar.HOUR, SUB_HOUR);
c.add(Calendar.MINUTE, SUB_MINUTE);
// Get the hour and the minute calculated
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
[...]
当然这意味着之前的任务:
hour = selectedHour;
minute = selectedMinute;
现在没用了,您可以删除它。
顺便说一下,你没有考虑到这一天,例如选择凌晨1:30将返回下午6:00但前一天。
编辑:我更正了我的代码。