选择时间为

时间:2015-06-18 12:42:23

标签: android android-timepicker

我在textview点击时打开时间选择器。 textview的值设置为12:0:0。但是当打开时间选择器时,它会显示当前时间。我希望它显示12:0:0,然后让用户更改它。我该如何实现?这是我的代码:

    mcurrentTime = Calendar.getInstance();

               int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
               int minute = mcurrentTime.get(Calendar.MINUTE);
               TimePickerDialog mTimePicker;
               mTimePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
                   @Override
                   public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    timePicker.setIs24HourView(true);

                    if(db.getAlarm1().contains(selectedHour + " : " + selectedMinute+" : 0")){
                        alarm1.setText( selectedHour + " : " +( selectedMinute+1)+" : 0"); 
                    }
                    else{
                      alarm1.setText( selectedHour + " : " + selectedMinute+" : 0");  
                    }
                       mcurrentTime.set(Calendar.HOUR_OF_DAY,selectedHour);
                       mcurrentTime.set(Calendar.MINUTE,selectedMinute);
                       mcurrentTime.set(Calendar.SECOND,0);
                     l= mcurrentTime.getTimeInMillis();
                    // alarm1.setText(String.valueOf(l)+" ../.. "+String.valueOf(Calendar.getInstance().getTimeInMillis()));

                   }
               }, hour, minute, true);//Yes 24 hour tim
               mTimePicker.setTitle("Select Time");
               mTimePicker.show();

3 个答案:

答案 0 :(得分:0)

TimePickerDialog构造函数中代替小时,分钟显示您想要显示的时间。

答案 1 :(得分:0)

您将相同的hourminute变量注入到从Calendar实例获得的片段中。因此,请尝试手动设置它们:

int hour = 12; 
int minute = 0;

答案 2 :(得分:0)

试试这段代码,它对我有用:

Calendar cal=Calendar.getInstance();

int year=cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH);
int day=cal.get(Calendar.DAY_OF_MONTH);
int hour=cal.get(Calendar.HOUR_OF_DAY);
int min=cal.get(Calendar.MINUTE);

datePicker.updateDate(year, month, day);

timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(min);