我正在尝试将TimePickerDialog设置为10分钟的间隔,我已经看到了各种解决方案看起来应该可以工作但是最优雅的解决方案似乎是从TimePickerDialog派生,然后将NumberPicker属性覆盖为00,10 ,20 ...... 40而不是00,01,02 ...... 59。
Java中的解决方案似乎是在派生类的构造函数中获取NumberPicker控件并重置其所有值以显示您想要的内容。这个解决方案可以在01.sunlit Set TimePickerDialog to use 15 minute intervals
找到我试图在Xamarin中实现此解决方案但我无法访问数字选择器对象。
您能告诉我如何在派生类的构造函数中访问此对象吗?
答案 0 :(得分:3)
所以我设法让它像这样工作:
public class TimePickerDialogIntervals : TimePickerDialog
{
public const int TimePickerInterval = 10;
private bool _ignoreEvent = false;
public TimePickerDialogIntervals(Context context, EventHandler<TimePickerDialog.TimeSetEventArgs> callBack, int hourOfDay, int minute, bool is24HourView)
: base(context, (sender, e) => {
callBack (sender, new TimePickerDialog.TimeSetEventArgs (e.HourOfDay, e.Minute * TimePickerInterval));
}, hourOfDay, minute/TimePickerInterval, is24HourView)
{
}
protected TimePickerDialogIntervals(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public override void SetView(View view)
{
SetupMinutePicker (view);
base.SetView(view);
}
void SetupMinutePicker (View view)
{
var numberPicker = FindMinuteNumberPicker (view as ViewGroup);
if (numberPicker != null) {
numberPicker.MinValue = 0;
numberPicker.MaxValue = 5;
numberPicker.SetDisplayedValues (new String[] { "00", "10", "20", "30", "40", "50" });
}
}
protected override void OnCreate (Android.OS.Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
GetButton((int)DialogButtonType.Negative).Visibility = Android.Views.ViewStates.Gone;
this.SetCanceledOnTouchOutside (false);
}
private NumberPicker FindMinuteNumberPicker(ViewGroup viewGroup)
{
for (var i = 0; i < viewGroup.ChildCount; i++)
{
var child = viewGroup.GetChildAt(i);
var numberPicker = child as NumberPicker;
if (numberPicker != null)
{
if (numberPicker.MaxValue == 59)
{
return numberPicker;
}
}
var childViewGroup = child as ViewGroup;
if (childViewGroup != null)
{
var childResult = FindMinuteNumberPicker (childViewGroup);
if(childResult !=null)
return childResult;
}
}
return null;
}
}