android – 以hh:mm格式显示时间选择器
作者:互联网
我必须以hh:mm格式显示时间选择器点击编辑文本.所以我使用了下面的代码.
MainActivity.java:
e6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(AddFlight.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
e6.setText(selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
它运行良好并显示时间选择器.但我的问题是,当我选择时间戳中的04:15时,它显示4:15.它之前不包括0.我需要在hh中显示时间选择器:mm格式.
任何人都可以帮助我.谢谢.
解决方法:
刚才我推荐了这个post并更改了单行:
这条线
e6.setText(selectedHour + “:” + selectedMinute);
至
e6.setText(String.format(“%02d:%02d”, selectedHour,
selectedMinute));
标签:timepicker,android,date 来源: https://codeday.me/bug/20191008/1871643.html