我有一个奇怪的问题。当我尝试使用Time-picker
选择时间时,我得到一个奇怪的结果。
例如,如果我将时间设置为11:04
,则输出为11:4
???
此外,如果我将时间设置为12:30
,则输出为12:3
???
//Time Picker Dialog
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case dialog_id:
return new TimePickerDialog(this, mTimeSetListener, hour, minute, false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int hour_minute)
{
hour = hourOfDay;
minute = hour_minute;
if (hourOfDay > 12)
{
hour = hourOfDay - 12;
amORpm = "PM";
}
else
{
hour = hourOfDay;
amORpm = "AM";
}
textBox.setText(hour + " : " + minute + " " + amORpm);
Toast.makeText(getBaseContext(), "Arrival time changed to: " + hour +":" + minute , Toast.LENGTH_LONG).show();
}
};
答案 0 :(得分:0)
尝试使用
minute = String.valueOf(hour_minute);
因为Integer省略了起始0,并确保分钟是字符串变量
答案 1 :(得分:0)
您需要使用以下格式正确显示一位数字时间:
String.format("%02d:%02d", hour, min);
其中小时和分钟表示小时和分钟的值。
答案 2 :(得分:0)
由于返回值是整数,因此排除前导零。如果你需要使用整数,比如设置闹钟,你就可以这样做。要显示给用户,您可以从日历中获取时间作为字符串,并将其存储在一个字符串中,该字符串将具有前导零,如02:05。得到这样的结果将返回值为零,但作为字符串:
String.format("%02d", minute);
答案 3 :(得分:-1)
使用下面的代码,它对我来说非常合适。
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour,
minute, false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
hour = hourOfDay;
minute = minutes;
updateTime(hour, minute);
}
};
private void updateTime(int hours, int mins) {
String timeSet = "";
if (hours > 12) {
hours -= 12;
timeSet = "PM";
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
} else if (hours == 12)
timeSet = "PM";
else
timeSet = "AM";
String minutes = "";
if (mins < 10)
minutes = "0" + mins;
else
minutes = String.valueOf(mins);
String aTime = new StringBuilder().append(hours).append(':').append(minutes)
.append(" ").append(timeSet).toString();
textBox.setText(aTime);
}
答案 4 :(得分:-2)
使用此课程
public class TimeListener implements OnClickListener, OnTimeSetListener {
private int hour;
private int minute;
private Activity activity;
private View touchedView;
public TimeListener(Activity activity) {
this.activity = activity;
final Calendar c = Calendar.getInstance();
this.hour = c.get(Calendar.HOUR_OF_DAY);
this.minute = c.get(Calendar.MINUTE);
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
@Override
public void onClick(View v) {
touchedView = v;
new TimePickerDialog(activity,
this, this.getHour(), this.getMinute(), false).show();
}
private void updateDisplay() {
((TextView) touchedView).setText(
new StringBuilder()
.append(pad(hour)).append(":")
.append(pad(minute)));
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
this.hour = hourOfDay;
this.minute = minute;
updateDisplay();
}
}
并使用下面的代码在您的活动类中......
private TimeListener timeListener = new TimeListener(this);
view.setOnClicklistner(timeListener);
谢谢...