OStack程序员社区-中国程序员成长平台

标题: android - Xamarin.Forms Timepicker IsVisible 属性与自定义渲染器 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:15
标题: android - Xamarin.Forms Timepicker IsVisible 属性与自定义渲染器

在 Xamarin.Forms 中,我使用 TimePicker 控件让用户选择时间以进行计划。为此,我使用自定义渲染器,因为我需要将分钟间隔设置为例如30 分钟和 24 小时制。这一切都很完美,除了样式问题。我的 TimePicker 需要在标签元素上不带边框显示(我并不关心对话框中的样式)

来自 iOS 的 native 实现会导致 TimePicker 出现一个完整的边框。 Android 的原生实现会导致 TimePicker 出现底部边框。

因为 Xamarin.Forms TimePicker 没有边框属性,并且我创建的两个自定义渲染器都无法隐藏边框,所以我选择最初通过设置属性 IsVisible=false 来隐藏 TimePicker,并改为显示标签控件。这可以满足样式问题的需要,但设置此属性会导致 Android 实现忽略间隔和 24 小时设置。 iOS 实现按预期工作。

关于如何解决此问题的任何建议?这里似乎是一个 Android 错误或不受欢迎的实现,但仍然......我看到的唯一其他选择是创建我自己的选择器实现,这是可能的并且不难,但不是我的首选......



Best Answer-推荐答案


我有一个实现,你可以试试这个方法:

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;
        }
    }
}

关于android - Xamarin.Forms Timepicker IsVisible 属性与自定义渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32314484/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4