I have used a 24 hours TimePicker in my Xamarin Forms project. The TimePicker works perfectly in my iOS project but on Android the UnFocused event is not called, also the PropertyChange event is not fired when I click on Ok or Cancel without changing the time.
The timepicker opens up with the time value binded in the xaml and I want the same time to be set on click of Ok without changing the time.
Say I have binded 7 hrs from xaml, TimePicker opens with 7:00 selected but if I click Ok and Cancel without changing the time it sets 0: 00.
Following is my 24 hrs TimePicker implementation in Android:
p ublic class TimePicker24Hours : ViewRenderer<TimePicker, Android.Widget.EditText>, TimePickerDialog.IOnTimeSetListener, IJavaObject, IDisposable
{
private TimePickerDialog dialog = null;
protected override void OnElementChanged(ElementChangedEventArgs<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();
}
void Control_Click(object sender, EventArgs e)
{
ShowTimePicker();
}
private void ShowTimePicker()
{
if (dialog == null)
{
dialog = new TimePickerDialog(Forms.Context, this, 0, 0, true);
}
dialog.Show();
}
public void OnTimeSet(Android.Widget.TimePicker view, int hourOfDay, int minute)
{
var time = new TimeSpan(hourOfDay, minute, 0);
this.Element.SetValue(TimePicker.TimeProperty, time);
this.Control.Text = time.ToString(@"hh\:mm");
}
The OnTimeSet method is called when I click on Ok or Cancel but sets 0 hrs 0 min if I don't change the time.
The FocusChangeevent in the android implementation is not getting fired when TimePicker is unfocused.