我有一个时间选择器功能,可以在EditText中设置时间。但它显示的格式不合适。例如,04:07 pm显示为4:7。只要时间上的数字小于10,它就会自动删除0。 请帮帮我。 我的代码是
if (v == btnTimePicker1)
{
// Process to get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// Display Selected time in textbox
txtTime1.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tpd.show();
}
答案 0 :(得分:57)
只需更改一行:
txtTime1.setText(hourOfDay + ":" + minute);
为:
txtTime1.setText(String.format("%02d:%02d", hourOfDay, minute));
一切都会好的。
如果你想要一个12小时制而不是24小时制,那么用这些代替那一行:
int hour = hourOfDay % 12;
if (hour == 0)
hour = 12;
txtTime1.setText(String.format("%02d:%02d %s", hour, minute,
hourOfDay < 12 ? "am" : "pm"));
或者你可以用以下两行来完成:
int hour = hourOfDay % 12;
txtTime1.setText(String.format("%02d:%02d %s", hour == 0 ? 12 : hour,
minute, hourOfDay < 12 ? "am" : "pm"));
答案 1 :(得分:4)
您可以检查小时和分钟是否小于10。 如果是这样,你只需添加&#34; 0&#34;面对那个特定的字符串。 只需像这样修改你的代码:
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// Display Selected time in textbox
String hourString;
if (hourOfDay < 10)
hourString = "0" + hourOfDay;
else
hourString = "" +hourOfDay;
String minuteSting;
if (minute < 10)
minuteSting = "0" + minute;
else
minuteSting = "" +minute;
txtTime1.setText(hourString + ":" + minuteSting);
}
答案 2 :(得分:3)
逻辑很简单,我刚刚修改了上面的答案
用
替换我们在editText中设置时间的行txtTime.setText(pad(hourOfDay) + ":" + pad(minute));
然后为它添加一个函数,即
public String pad(int input)
{
String str = "";
if (input > 10) {
str = Integer.toString(input);
} else {
str = "0" + Integer.toString(input);
}
return str;
}
答案 3 :(得分:2)
首先需要创建一个检查输入的函数,并根据条件将其转换为String。
public String pad(int input) {
if (input >= 10) {
return String.valueOf(input);
} else {
return "0" + String.valueOf(input);
}
}
然后你可以像这样打电话
txtTime1.setText(pad(hourOfDay) + ":" + pad(minute));
答案 4 :(得分:1)
您可以使用以下代码 -
public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfDay) {
int hour;
String minute, amOrPm;
if (hourOfDay > 12) {
hour = hourOfDay - 12;
amOrPm= "PM";
} else {
hour = hourOfDay;
amOrPm = "AM";
}
if(minuteOfDay < 10) {
minute = "0"+minuteOfDay;
} else {
minute = "" + minuteOfDay;
}
txtTime1.setText(hour + " : " + minute + " " + amOrPm);
}
答案 5 :(得分:0)
SubContent
答案 6 :(得分:0)
试试这个
simpleTimePicker1 = (TimePicker) findViewById(R.id.timedawn);
simpleTimePicker1.setIs24HourView(true);
simpleTimePicker1.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay1, int minute1 ) {
String shour1 = String.valueOf(hourOfDay1);
String sminute1 = String.valueOf(minute1);
if ((shour1.length() == 1 ) && (sminute1.length() == 1)) {
sHour1= "0" + shour1;
sMinute1= "0" + sminute1;
}
else if ((shour1.length() == 2 ) && (sminute1.length() == 1)) {
sHour1= shour1;
sMinute1= "0" + sminute1;
}
else if ((shour1.length() == 1 ) && (sminute1.length() == 2)) {
sHour1= "0" + shour1;
sMinute1= sminute1;
}
else{
sHour1= shour1;
sMinute1= sminute1;
}
String timew = sHour1+":" + sMinute1;
time3=timew;
}
});
答案 7 :(得分:0)
我参加聚会可能有点晚,但这肯定会对其他人有所帮助。
private String getTime(int hr, int min) {
Time tme = new Time(hr, min, 0);//seconds by default set to zero
Format formatter;
formatter = new SimpleDateFormat("h:mm a");
return formatter.format(tme);
}