似乎TimePicker由三个内部NumberPickers组成。是否可以访问它们?需要访问和修改它们中的每一个。
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
TimePicker dialog = new TimePicker(getActivity(), this, 22, 30,
DateFormat.is24HourFormat(getActivity()));
// Create a new instance of TimePickerDialog and return it
return dialog;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
在对话框中,我创建了一个TimePicker
来显示,但我必须修改TimePicker
分钟数列(旋转器)并添加15分钟间隔。我想知道是否有办法单独访问那些NumberPickers。
更新 我在下面发布了答案,但是这对我来说是Android内置组件中最难的修改之一,我终于找到了答案并在这里分享了,但idk为什么投票失败了?
答案 0 :(得分:0)
最后我找到了解决方案。我在这里获取分钟列,并在那里设置15分钟间隔。
changeTimepicker(R.id.from_time_picker, 15);
方法实现:
try {
Class<?> rClass = Class.forName("com.android.internal.R$id");
// Field timePicker = rClass.getField(name);
TimePicker mTimePicker = (TimePicker) findViewById(id);
Field m = rClass.getField("minute");
NumberPicker mMinuteSpinner = (NumberPicker) mTimePicker.findViewById(m.getInt(null));
mMinuteSpinner.setMinValue(0);
mMinuteSpinner.setMaxValue((60 / increment) - 1);
List<String> displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += increment) {
displayedValues.add(String.format("%02d", i));
}
mMinuteSpinner.setDisplayedValues(displayedValues.toArray(new String[0]));
} catch (Exception e) {
e.printStackTrace();
}
}