我是android新手,在我的应用中,我这里有一个时间选择器,我需要以编程方式设置小时,分钟,AM / PM值。
时间值是“ 11:11:AM”,在此我设置了小时和时间,并且工作正常。但是我不知道如何设置AM / PM值。
代码:
if (!(mTime == null)) {
String timme = mTime;
String[] time = timme.split(":");
int hour = Integer.parseInt(time[0].trim());
int min = Integer.parseInt(time[1].trim());
String amPm = ((time[2].trim()));
mTimePicker.setIs24HourView(false);
mTimePicker.setHour(hour);
mTimePicker.setMinute(min);
}
图片:
即使字符串具有“ PM”,它也始终显示“ AM”。 谁能帮我解决这个问题。
答案 0 :(得分:1)
答案 1 :(得分:0)
Android会根据24小时格式提供的时间自动设置AM / PM。要澄清:
您必须通过调用
将TimePicker设置为12小时模式#!/usr/bin/python3
# ./dataparse.py
from __future__ import print_function
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
import re
with open('test2', 'r') as f:
for line in f:
line = line.strip()
data = f.read()
regex = (r"dn:(.*?)\nftpuser: (.*)\ndescription:* (.*)")
matchObj = re.findall(regex, data)
for index in matchObj:
#print(index)
index_str = ' '.join(index)
new_str = re.sub(r'[=,]', ' ', index_str)
new_str = new_str.split()
print("{0:<30}{1:<20}{2:<50}".format(new_str[1],new_str[8],new_str[9]))
,然后使用24小时制提供时间
$ ./dataparse.py
ab02 disabled_5Mar07 Remedy
mela Y ROYALS|none|customer
ab01 Y VGVzdGluZyA
tt@regg.com T REG-JP|7-31-05|REG-JP
如果我是你,我将把字符串拆开,检查最后一部分是AM还是PM,如果是PM,我会在小时值上加上12,因此如果是yourtimepicker.setIs24HourView(false)
,我会得到23小时,它到android,并让它处理其他所有事情。
注意:这适用于微调器和时钟模式
答案 2 :(得分:0)
String = aTime
是24 hour
格式的字符串
mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String timeSet;
if (selectedHour > 12) {
selectedHour -= 12;
timeSet = "PM";
} else if (selectedHour == 0) {
selectedHour += 12;
timeSet = "AM";
} else if (selectedHour == 12)
timeSet = "PM";
else
timeSet = "AM";
String minutes = "";
if (selectedMinute < 10)
minutes = "0" + selectedMinute;
else
minutes = String.valueOf(selectedMinute);
String aTime = new StringBuilder().append(selectedHour).append(':')
.append(minutes).append(" ").append(timeSet).toString();
relatedWorkTime.setText(aTime);
}
}, hour, minute, false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
答案 3 :(得分:0)
使用以下代码将24小时制转换为12小时制,并添加AM / PM。
String time = ((hourOfDay > 12) ? hourOfDay % 12 : hourOfDay) + ":" + (minute < 10 ? ("0" + minute) : minute) + " " + ((hourOfDay >= 12) ? "PM" : "AM");
请在下面找到android timepicker的完整代码。
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(CreateNewBatchActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
String time = ((hourOfDay > 12) ? hourOfDay % 12 : hourOfDay) + ":" + (minute < 10 ? ("0" + minute) : minute) + " " + ((hourOfDay >= 12) ? "PM" : "AM");
buttonBatchStartTime.setText(time);
}
}, mHour, mMinute, false);
timePickerDialog.show();