gpt4 book ai didi

c# - 24 小时 TimePicker,带有聚焦和非聚焦事件

转载 作者:行者123 更新时间:2023-11-30 21:35:30 27 4
gpt4 key购买 nike

我尝试实现 24 小时 Time Picker,如此处 24 Hour TimePicker在 XF 项目中。

但它不会触发 Android 上的 FocusUnfocused 事件。有人实现了吗?

最佳答案

这是一个完整的示例:https://github.com/brminnick/24HourTimePicker

说明

此自定义控件的作者忘记触发 FocusedUnfocused在 Android 自定义渲染器中。

我们可以通过调用 ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, [true/false]); 来触发这些事件来自 Android Custom Renderer when FocusChanged火灾。

此自定义 Android 控件在设置时间后也不会清除焦点,因此我们将添加 ClearFocus()OnTimeSet也是。

Xamarin.Forms 2.4 代码

因为示例是用 Xamarin.Forms v2.4 编写的,所以如果您继续使用 Xamarin.Forms v2.4,这里就是答案。

using System;

using Android.App;
using Android.Content;
using Android.Runtime;

using TimePickerDemo.CustomControls;
using TimePickerDemo.Droid;

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

[assembly: ExportRenderer(typeof(CustomTimePicker24H), typeof(CustomTimePicker24HRenderer))]
namespace TimePickerDemo.Droid
{
public class CustomTimePicker24HRenderer : ViewRenderer<Xamarin.Forms.TimePicker, Android.Widget.EditText>, TimePickerDialog.IOnTimeSetListener, IJavaObject, IDisposable
{
private TimePickerDialog dialog = null;

IElementController ElementController => Element as IElementController;

protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
{
base.OnElementChanged(e);
this.SetNativeControl(new Android.Widget.EditText(Forms.Context));
this.Control.Click += Control_Click;
this.Control.Text = DateTime.Now.ToString("HH:mm");
this.Control.KeyListener = null;
this.Control.FocusChange += Control_FocusChange;
}

void Control_FocusChange(object sender, Android.Views.View.FocusChangeEventArgs e)
{
if (e.HasFocus)
{
ShowTimePicker();
ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);
}
else
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
}
}

void Control_Click(object sender, EventArgs e)
{
ShowTimePicker();
}

private void ShowTimePicker()
{
if (dialog == null)
{
dialog = new TimePickerDialog(Forms.Context, this, DateTime.Now.Hour, DateTime.Now.Minute, true);
}

dialog.Show();
}

public void OnTimeSet(Android.Widget.TimePicker view, int hourOfDay, int minute)
{
var time = new TimeSpan(hourOfDay, minute, 0);
this.Element.SetValue(Xamarin.Forms.TimePicker.TimeProperty, time);

this.Control.Text = time.ToString(@"hh\:mm");

this.ClearFocus();
}
}
}

Xamarin.Forms 2.5+ 答案

Xamarin.Forms v2.5 已弃用 Forms.Context并且还需要一个重载的构造函数用于自定义渲染器。

替换Forms.Context ,我们需要安装 Plugin.CurrentActivity NuGet Package .

using System;

using Android.App;
using Android.Content;
using Android.Runtime;

using TimePickerDemo.CustomControls;
using TimePickerDemo.Droid;

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

[assembly: ExportRenderer(typeof(CustomTimePicker24H), typeof(CustomTimePicker24HRenderer))]
namespace TimePickerDemo.Droid
{
public class CustomTimePicker24HRenderer : ViewRenderer<Xamarin.Forms.TimePicker, Android.Widget.EditText>, TimePickerDialog.IOnTimeSetListener, IJavaObject, IDisposable
{
private TimePickerDialog dialog = null;

public CustomTimePicker24HRenderer(Context context) : base(context)
{

}

Context CurrentContext => Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity;
IElementController ElementController => Element as IElementController;

protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
{
base.OnElementChanged(e);
this.SetNativeControl(new Android.Widget.EditText(CurrentContext));
this.Control.Click += Control_Click;
this.Control.Text = DateTime.Now.ToString("HH:mm");
this.Control.KeyListener = null;
this.Control.FocusChange += Control_FocusChange;
}

void Control_FocusChange(object sender, Android.Views.View.FocusChangeEventArgs e)
{
if (e.HasFocus)
{
ShowTimePicker();
ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);
}
else
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
}
}

void Control_Click(object sender, EventArgs e)
{
ShowTimePicker();
}

private void ShowTimePicker()
{
if (dialog == null)
{
dialog = new TimePickerDialog(CurrentContext, this, DateTime.Now.Hour, DateTime.Now.Minute, true);
}

dialog.Show();
}

public void OnTimeSet(Android.Widget.TimePicker view, int hourOfDay, int minute)
{
var time = new TimeSpan(hourOfDay, minute, 0);
this.Element.SetValue(Xamarin.Forms.TimePicker.TimeProperty, time);

this.Control.Text = time.ToString(@"hh\:mm");

this.ClearFocus();
}
}
}

关于c# - 24 小时 TimePicker,带有聚焦和非聚焦事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49158815/

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