在Xamarin.Forms中,我使用TimePicker控件让用户选择时间进行调度。我为此目的使用自定义渲染器,因为我需要将分钟间隔设置为例如30分钟和24小时制。除样式问题外,这一切都很完美。我的TimePicker需要在标签元素上显示没有边框(我并不真正关心对话框中的样式)
来自iOS的本机实现会导致TimePicker以全边框显示。 Android中的Native Implementation会导致TimePicker以底部边框显示。因为Xamarin.Forms TimePicker没有border属性而且我创建的两个Custom Renderers都无法隐藏边框,所以我选择通过设置属性IsVisible = false来初始隐藏TimePicker并显示一个标签控件代替。这适用于样式问题,但设置此属性会导致Android实现省略Interval和24小时设置。 iOS实现可以根据需要运行。
有关如何解决此问题的任何建议?这看起来像Android漏洞或不受欢迎的实现,但仍然......我看到的其他选项只是创建我自己的选择器实现,这可能并不难,但不是我的第一选择......
答案 0 :(得分:1)
我有一个实现,你可以尝试这种方法:
using Android.Views;
using Android.Widget;
using Android.Content;
using Android.App;
using Android.Runtime;
[assembly: ExportRenderer (typeof (Xamarin.Forms.TimePicker), typeof (CustomTimePicker))]
namespace Consulta_Medica_Medico.Droid
{
public class CustomTimePicker : TimePickerRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
{
base.OnElementChanged (e);
TimePickerDialogIntervals timePickerDlg = new TimePickerDialogIntervals (this.Context,
new EventHandler<TimePickerDialogIntervals.TimeSetEventArgs> (UpdateDuration),
Element.Time.Hours, Element.Time.Minutes, true);
var control = new EditText (this.Context);
control.Focusable = false;
control.FocusableInTouchMode = false;
control.Clickable = false;
control.Click += (sender, ea) => timePickerDlg.Show ();
control.Text = Element.Time.Hours.ToString ("00") + ":" + Element.Time.Minutes.ToString ("00");
SetNativeControl (control);
}
void UpdateDuration(object sender, Android.App.TimePickerDialog.TimeSetEventArgs e)
{
Element.Time = new TimeSpan (e.HourOfDay, e.Minute, 0);
Control.Text = Element.Time.Hours.ToString ("00") + ":" + Element.Time.Minutes.ToString ("00");
}
}
public class TimePickerDialogIntervals : TimePickerDialog
{
public static int TimePickerInterval = App.curUser.TimeInterval.Minutes;
//private bool _ignoreEvent = false;
public TimePickerDialogIntervals(Context context,
EventHandler<TimePickerDialog.TimeSetEventArgs> callBack, int hourOfDay, int minute, bool is24HourView)
: base(context, (sender, e) => {
try{
switch (App.curUser.TimeInterval.Minutes) {
case 15:
TimePickerInterval = 15;
callBack (sender, new TimePickerDialog.TimeSetEventArgs (e.HourOfDay, e.Minute * TimePickerInterval));
break;
case 20:
TimePickerInterval = 20;
callBack (sender, new TimePickerDialog.TimeSetEventArgs (e.HourOfDay, e.Minute * TimePickerInterval));
break;
case 30:
TimePickerInterval = 30;
callBack (sender, new TimePickerDialog.TimeSetEventArgs (e.HourOfDay, e.Minute * TimePickerInterval));
break;
case 45:
TimePickerInterval = 45;
callBack (sender, new TimePickerDialog.TimeSetEventArgs (e.HourOfDay, e.Minute * TimePickerInterval));
break;
case 0:
callBack (sender, new TimePickerDialog.TimeSetEventArgs (e.HourOfDay, e.Minute));
break;
}
}
catch (Exception ex){
Console.WriteLine(ex.Message);
}
}, hourOfDay, TimePickerInterval == 0 ? minute : minute/TimePickerInterval, is24HourView)
{
}
protected TimePickerDialogIntervals(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public override void SetView(Android.Views.View view)
{
SetupMinutePicker (view);
base.SetView(view);
}
void SetupMinutePicker (Android.Views.View view)
{
var numberPicker = FindMinuteNumberPicker (view as ViewGroup);
if (numberPicker != null) {
numberPicker.MinValue = 0;
if (App.curUser.TimeInterval.Minutes != 0) {
numberPicker.MaxValue = (60 / App.curUser.TimeInterval.Minutes) - 1;
if(App.curUser.TimeInterval.Minutes == 45)
numberPicker.MaxValue = 3;
} else {
numberPicker.MaxValue = 0;
}
switch (App.curUser.TimeInterval.Minutes) {
case 15:
numberPicker.SetDisplayedValues (new String[] {"00","15","30","45"});
break;
case 20:
numberPicker.SetDisplayedValues (new String[] {"00","20","40"});
break;
case 30:
numberPicker.SetDisplayedValues (new String[] {"00","30"});
break;
case 45:
numberPicker.SetDisplayedValues (new String[] {"00","15","30","45"});
break;
case 0:
numberPicker.SetDisplayedValues (new String[] {"00"});
break;
}
}
}
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;
}
}
}