Android Timepicker麻烦

时间:2015-05-06 15:03:50

标签: android timepicker

我尝试从TimePicker窗口小部件中提取所选时间,但是当我使用getCurrentHour()getCurrentMinute()方法时,它会保留当前时间而不是我选择了。

`

公共类TimerClockActivity扩展了Activity {

private Calendar calendar;
private AlarmManager alarmManager;
private PendingIntent pendingIntent;
private TimePicker timePickerOn;
private TimePicker timePickerOff;
private ToggleButton tbCircuitOn;
private ToggleButton tbCircuitOff;
private static AlarmActivity alarmActivity;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer_clock);

calendar = calendar.getInstance();

    timePickerOn = (TimePicker) findViewById(R.id.timePickerOn);
    timePickerOff = (TimePicker) findViewById(R.id.timePickerOff);
    tbCircuitOn = (ToggleButton) findViewById(R.id.tbCircuitOn);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    tbCircuitOn.setOnClickListener(new ClickListener());


}


private class ClickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.tbCircuitOn) {
            if (tbCircuitOn.isChecked()) {
                // turn alarm on



                timePickerOn.clearFocus();

                Log.d("Hour", String.valueOf(timePickerOn.getCurrentHour()));
                Log.d("Min", String.valueOf(timePickerOn.getCurrentMinute()));



                calendar.set(Calendar.HOUR_OF_DAY, timePickerOn.getCurrentHour());
                calendar.set(Calendar.MINUTE, timePickerOn.getCurrentMinute());

                Log.d("The Time", calendar.toString());
                Log.d("Hour", String.valueOf(timePickerOn.getCurrentHour()));
                Log.d("Min", String.valueOf(timePickerOn.getCurrentMinute()));


                Intent myIntent = new Intent(TimerClockActivity.this, AlarmHelper.class);
                pendingIntent = PendingIntent.getBroadcast(TimerClockActivity.this, 0, myIntent,0);

                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);



            } else {
                calendar.clear();
            }

        }


    }
}

}

`

并且已经选择了与登录的logcat显示的不同时间

05-06 16:18:49.282 25999-25999/com.me.raspiapp D/﹕ Surface destroy: ANDROID_NATIVE_WINDOW_MAGIC 05-06 16:18:54.142 25999-25999/com.me.raspiapp D/Hour﹕ 16 05-06 16:18:54.142 25999-25999/com.adamgelston.raspiapp D/Min﹕ 18 05-06 16:18:54.142 25999-25999/com.adamgelston.raspiapp D/The Time﹕ java.util.GregorianCalendar[time=?,areFieldsSet=false,lenient=true,zone=Europe/Dublin,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=4,WEEK_OF_YEAR=19,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,DAY_OF_YEAR=126,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=18,SECOND=49,MILLISECOND=103,ZONE_OFFSET=0,DST_OFFSET=3600000] 05-06 16:18:54.142 25999-25999/com.me.raspiapp D/Hour﹕ 16 05-06 16:18:54.142 25999-25999/com.me.raspiapp D/Min﹕ 18

有任何修复建议吗?

1 个答案:

答案 0 :(得分:1)

您应该使用回复OnTimeSetListener

的时间
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  // Do something with the time chosen by the user
}

您可以看到示例HERE

显然你需要强制它失去焦点,然后更新值

// force the timepicker to loose focus and the typed value is available !
timePicker.clearFocus();
// re-read the values, in my case i put them in a Time object.
time.hour   = timePicker.getCurrentHour();
time.minute = timePicker.getCurrentMinute();