timepickerdialog分钟输入错误

时间:2016-09-23 09:57:49

标签: android datetime android-studio

private TimePickerDialog.OnTimeSetListener timeListener = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        if(hourOfDay < 10){
            strHourOfDay = "0" + hourOfDay;
        } else { strHourOfDay = String.valueOf(hourOfDay);}

        if(minute < 10){
            strMinute = "0" + minute;
        } else { strMinute = String.valueOf(minute);}

        period.put("hour",strHourOfDay);
        period.put("minute",strMinute);

        periodTime.setText(strHourOfDay + ":" + strMinute);
    }
};

我使用TimePickerDialog来节省时间和分钟。 在strHourOfDay中成功输入hourOfDay。但分钟未能在strMinute中输入..

调试结果在图片下

enter image description here

有什么不对?

1 个答案:

答案 0 :(得分:0)

始终使用;

String.valueOf(minute);

如果要将数字(float,double,int,long)解析为String

所以你的代码应该是这样的:

if(minute < 10) {
        strMinute = "0" + String.valueOf(minute);
} else { 
      strMinute = String.valueOf(minute);
}