1 /** 2 * 设置时间选择器 3 */ 4 private void setTimePickerDialog() { 5 Calendar mCalendar = Calendar.getInstance(); 6 mCalendar.setTimeInMillis(System.currentTimeMillis()); 7 final int hour = mCalendar.get(Calendar.HOUR_OF_DAY); 8 int minute = mCalendar.get(Calendar.MINUTE); 9 new TimePickerDialog(this,new TimePickerDialog.OnTimeSetListener() { 10 @Override 11 public void onTimeSet(TimePicker view,int hourOfDay,int minute) 12 { 13 //使用三元运算符来满足需求 14 String sHour = hour < 10 ? "0" + hour : "" + hour; 15 String sMinutes = minute < 10 ? "0" + minute : "" 16 + minute; 17 //指定一个textview来显示时间 18 textview.setText(sHour + ":" + sMinutes); 19 } 20 }, hour, minute, true).show(); 21 }