gpt4 book ai didi

java - Android TimePicker 未给出所需结果

转载 作者:行者123 更新时间:2023-12-01 15:06:18 24 4
gpt4 key购买 nike

您好,我正在尝试计算用户的 sleep 时间为 7.5。该应用程序应建议用户应在准确的时间上床 sleep 。例如,她选择上午 7:00 作为她的起床时间,应用程序会建议她在上午 11:30 之前去 sleep 。到目前为止我尝试过的是:

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:00 作为起床时间时,结果是下午 2:30。但应该是中午 12:30。如有任何帮助,我们将不胜感激,谢谢。

最佳答案

不,这是正确的。如果您输入 7:00am + 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,但是是前一天。

编辑:我更正了我的代码。

关于java - Android TimePicker 未给出所需结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12901848/

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