Xamarin.forms TimePickerDialog Android 键盘输入

Xamarin.forms TimePickerDialog Android KeyBoard input

我正在使用 Xamarin.Forms 具有自定义渲染器功能的 TimePicker。当用户使用 TimePickerDialog 的默认滚动功能时,一切正常。

问题是我想在用户点击 TimePickerDialog 以 HH:mm 格式更新时间时隐藏 Android 键盘。

如果有任何帮助和指导,我将不胜感激。

您可以在 Custom Renderer

中设置 EditTextInputType
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
        {
            base.OnElementChanged(e);

            if(Control!=null)
            {
             
               Control.InputType = Android.Text.InputTypes.Null;
               

            }
        }

更新

如果在 Android 中创建自定义 TimePicker,您还可以设置 TimePicker 的 属性 DescendantFocusability like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

using xxx.Droid;

[assembly:ExportRenderer(typeof(Xamarin.Forms.TimePicker),typeof(MyTimePickerRenderer))]
namespace xxx.Droid
{
    public class MyTimePickerRenderer : TimePickerRenderer
    {
        Context Context;

        IElementController ElementController => Element as IElementController;

        public MyTimePickerRenderer(Context context) : base(context)
        {
            Context = context;
        }


        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
        {
            base.OnElementChanged(e);

            if(Control!=null)
            {

                //  Control.InputType = Android.Text.InputTypes.Null;

                Control.Click += Control_Click;

            }
        }

        private AlertDialog _dialog;


        private void Control_Click(object sender, EventArgs e)
        {
            Xamarin.Forms.TimePicker model = Element;

            Android.Widget.TimePicker picker = new Android.Widget.TimePicker(Context);

            picker.DescendantFocusability = DescendantFocusability.BlockDescendants;  // block keyboard here 

           var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
            layout.AddView(picker);

            ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);

            var builder = new AlertDialog.Builder(Context);
            builder.SetView(layout);


          
            builder.SetNegativeButton("Cancel  ", (s, a) =>
            {
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                // It is possible for the Content of the Page to be changed when Focus is changed.
                // In this case, we'll lose our Control.
                Control?.ClearFocus();
                _dialog = null;
            });
            builder.SetPositiveButton("Ok ", (s, a) =>
            {

                ElementController.SetValueFromRenderer(Xamarin.Forms.TimePicker.TimeProperty, new TimeSpan(picker.Hour,picker.Minute,0));
                // It is possible for the Content of the Page to be changed on SelectedIndexChanged.
                // In this case, the Element & Control will no longer exist.
                if (Element != null)
                {
                    
                    ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                    // It is also possible for the Content of the Page to be changed when Focus is changed.
                    // In this case, we'll lose our Control.
                    Control?.ClearFocus();
                }

                _dialog = null;
            });

            _dialog = builder.Create();
            _dialog.DismissEvent += (ssender, args) =>
            {
                ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
            };
            _dialog.Show();
        }
    }
}