gpt4 book ai didi

Android N、L 及以下时间选择器,具有分钟间隔

转载 作者:行者123 更新时间:2023-11-30 01:09:51 43 4
gpt4 key购买 nike

我使用的是 Timepicker,间隔从 this 开始。回答。我在 Andorid N 预览版 5 模拟器上运行了它,显然 THEME_HOLO_LIGHT Timepicker 已被删除。

如何让 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);
}
}
}
}

关于Android N、L 及以下时间选择器,具有分钟间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38530449/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com