标题可能有点令人困惑。我有一个 TimePicker 供用户选择事件的持续时间,我不想获取用户输入的每个值,保存它,然后将 TimePicker 的默认值设置为用户选择次数最多的时间,所以这可能会节省他的时间。
例如,如果用户不断选择1小时和45分钟,当他返回添加另一个事件时,它将在1小时和45分钟内,到目前为止我还没有尝试任何东西,因为我不知道如何,我只知道如何定义TimePicker的起始值。
timePickerDuration.setIs24HourView(true);
timePickerDuration.setCurrentHour(1);
timePickerDuration.setCurrentMinute(30);
最佳答案
根据您的尝试,
如果您的 HoursList
ArrayList 包含正确的值,那么您可以像这样循环并找到最常选择的小时,
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer i : HoursList) {
Integer count = map.get(i);
map.put(i, count != null ? count+1 : 0);
}
Integer mostlySelectedhr = Collections.max(map.entrySet(),
new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
}).getKey();
最后,moSTLySelectedhr
是最常选择的时间。
关于java - 将用户在 TimePicker 中选择的所有值放入列表中并读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37907841/