1
我正在使用Timepicker
與this答案的分鐘間隔。我在Andorid N預覽版5仿真器上運行了這個程序,顯然THEME_HOLO_LIGHT
Timepicker
已被刪除。Android N,L及以下分鐘間隔時間選擇器
我怎樣纔能有分鐘間隔的Timepicker
?對我來說,它似乎不太可能會回到Android N的最終版本。
我正在使用Timepicker
與this答案的分鐘間隔。我在Andorid N預覽版5仿真器上運行了這個程序,顯然THEME_HOLO_LIGHT
Timepicker
已被刪除。Android N,L及以下分鐘間隔時間選擇器
我怎樣纔能有分鐘間隔的Timepicker
?對我來說,它似乎不太可能會回到Android N的最終版本。
確實創建我自己的時間選擇器是解決方案。我爲其他人發佈我的解決方案。這會爲api 14及更高版本創建一個分鐘間隔的時間選擇器對話框。它的工作時間間隔爲1,5和15.其他值也可能起作用。
用法:
Dialog intervalTimePickerDialog = IntervalTimePickerDialogFactory.getIntervalTimePickerDialog(Context context, int positiveTextId, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, final int interval);
intervalTimePickerDialog .show();
我認爲它不會需要太多的解釋。 int positiveTextId
可以刪除,這只是我需要的東西。
timepickerdialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TimePicker
android:timePickerMode="spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timePicker"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/timePicker"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
style="?android:attr/borderlessButtonStyle"
android:id="@+id/buttonNegative" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inklokken"
style="?android:attr/borderlessButtonStyle"
android:id="@+id/buttonPositive" />
</LinearLayout>
</RelativeLayout>
代碼:
public class IntervalTimePickerDialogFactory {
public static Dialog getIntervalTimePickerDialog(Context context, int positiveTextId, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, final int interval){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
return new LIntervalTimePickerDialog(context, positiveTextId, callBack, hourOfDay, minute, is24HourView, interval);
}else{
return new IntervalTimePickerDialog(context, positiveTextId, callBack, hourOfDay, minute, is24HourView, interval);
}
}
private static class IntervalTimePickerDialog extends TimePickerDialog {
private TimePicker timePicker;
private final OnTimeSetListener callback;
private int interval;
public IntervalTimePickerDialog(Context context, int positiveTextId, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, final int interval) {
super(context, TimePickerDialog.THEME_HOLO_LIGHT, callBack, hourOfDay, ((int)Math.ceil((double)minute/(double)interval)), is24HourView);
this.interval = interval;
this.callback = callBack;
this.setButton(BUTTON_POSITIVE, context.getResources().getString(positiveTextId), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (callback != null && timePicker != null) {
timePicker.clearFocus();
callback.onTimeSet(timePicker, timePicker.getCurrentHour(),
timePicker.getCurrentMinute() * interval);
}
}
});
this.setButton(BUTTON_NEGATIVE, context.getResources().getString(R.string.annuleren), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
}
@Override
protected void onStop() {
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
try {
Class<?> classForid = Class.forName("com.android.internal.R$id");
Field timePickerField = classForid.getField("timePicker");
this.timePicker = (TimePicker) findViewById(timePickerField
.getInt(null));
Field field = classForid.getField("minute");
NumberPicker mMinuteSpinner = (NumberPicker) timePicker
.findViewById(field.getInt(null));
mMinuteSpinner.setMinValue(0);
mMinuteSpinner.setMaxValue((60/interval) - 1);
List<String> displayedValues = new ArrayList<>();
for (int i = 0; i < 60; i += interval) {
displayedValues.add(String.format("%02d", i));
}
mMinuteSpinner.setDisplayedValues(displayedValues
.toArray(new String[0]));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static class LIntervalTimePickerDialog extends Dialog{
private static final DecimalFormat FORMATTER = new DecimalFormat("00");
private final TimePickerDialog.OnTimeSetListener callback;
private int interval;
private TimePicker timePicker;
private NumberPicker minutePicker;
public LIntervalTimePickerDialog(Context context, int positiveTextId, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView, final int interval) {
super(context);
setContentView(R.layout.timepickerdialog);
timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(is24HourView);
timePicker.setCurrentHour(hourOfDay);
this.callback = callBack;
this.interval = interval;
Button buttonPositive = (Button) findViewById(R.id.buttonPositive);
buttonPositive.setText(positiveTextId);
buttonPositive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (callback != null && timePicker != null) {
timePicker.clearFocus();
callback.onTimeSet(timePicker, timePicker.getCurrentHour(),
timePicker.getCurrentMinute() * interval);
}
dismiss();
}
});
Button buttonNegative = (Button) findViewById(R.id.buttonNegative);
buttonNegative.setText(context.getResources().getString(R.string.annuleren));
buttonNegative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
setMinutePicker();
timePicker.setCurrentMinute((int) Math.ceil((double) minute/(double) interval));
}
public void setMinutePicker() {
int numValues = 60/interval;
String[] displayedValues = new String[numValues];
for (int i = 0; i < numValues; i++) {
displayedValues[i] = FORMATTER.format(i * interval);
}
View minute = timePicker.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));
if ((minute != null) && (minute instanceof NumberPicker)) {
minutePicker = (NumberPicker) minute;
minutePicker.setMinValue(0);
minutePicker.setMaxValue(numValues - 1);
minutePicker.setDisplayedValues(displayedValues);
}
}
}
}
使用'NumberPicker'自己創建。或者,[找到一個具有符合您需求的選取器的圖書館](http://android-arsenal.com/tag/27)。 – CommonsWare