Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 7e65138

Browse files
pauldipietrormarinho
authored andcommitted
[Android] TimePicker unfocuses on cancel (#238)
Related to the prior fix of the DatePicker not unfocusing on the cancel button being pressed, the TimePicker was not unfocusing, as well. A similar fix has been applied.
1 parent 3d431ad commit 7e65138

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
3+
using Xamarin.Forms.CustomAttributes;
4+
using Xamarin.Forms.Internals;
5+
6+
#if UITEST
7+
using Xamarin.UITest;
8+
using NUnit.Framework;
9+
#endif
10+
11+
namespace Xamarin.Forms.Controls.Issues
12+
{
13+
[Preserve(AllMembers = true)]
14+
[Issue(IssueTracker.Bugzilla, 42074, "[Android] Clicking cancel on a TimePicker does not cause it to unfocus", PlatformAffected.Android)]
15+
public class Bugzilla42074 : TestContentPage
16+
{
17+
const string TimePicker = "TimePicker";
18+
19+
protected override void Init()
20+
{
21+
var timePicker = new TimePicker
22+
{
23+
AutomationId = TimePicker
24+
};
25+
var timePickerFocusButton = new Button
26+
{
27+
Text = "Click to focus TimePicker",
28+
Command = new Command(() => timePicker.Focus())
29+
};
30+
Content = new StackLayout
31+
{
32+
Children =
33+
{
34+
timePicker,
35+
timePickerFocusButton
36+
}
37+
};
38+
}
39+
40+
#if UITEST
41+
42+
#if __ANDROID__
43+
[Test]
44+
public void TimePickerCancelShouldUnfocus()
45+
{
46+
RunningApp.Tap(q => q.Marked(TimePicker));
47+
RunningApp.WaitForElement(q => q.Marked("Cancel"));
48+
49+
RunningApp.Tap(q => q.Marked("Cancel"));
50+
RunningApp.WaitForElement(q => q.Marked("Click to focus TimePicker"));
51+
52+
RunningApp.Tap(q => q.Marked("Click to focus TimePicker"));
53+
RunningApp.WaitForElement(q => q.Marked("Cancel"));
54+
55+
RunningApp.Tap(q => q.Marked("Cancel"));
56+
}
57+
#endif
58+
59+
#endif
60+
}
61+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla41078.cs" />
109109
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla40998.cs" />
110110
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla41424.cs" />
111+
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla42074.cs" />
111112
<Compile Include="$(MSBuildThisFileDirectory)CarouselAsync.cs" />
112113
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla34561.cs" />
113114
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla34727.cs" />

Xamarin.Forms.Platform.Android/Renderers/TimePickerRenderer.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ void TimePickerDialog.IOnTimeSetListener.OnTimeSet(ATimePicker view, int hourOfD
2727

2828
ElementController.SetValueFromRenderer(TimePicker.TimeProperty, new TimeSpan(hourOfDay, minute, 0));
2929
Control.ClearFocus();
30+
31+
if (Forms.IsLollipopOrNewer)
32+
_dialog.CancelEvent -= OnCancelButtonClicked;
33+
3034
_dialog = null;
3135
}
3236

@@ -70,6 +74,10 @@ internal override void OnFocusChangeRequested(object sender, VisualElement.Focus
7074
_dialog.Hide();
7175
ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
7276
Control.ClearFocus();
77+
78+
if (Forms.IsLollipopOrNewer)
79+
_dialog.CancelEvent -= OnCancelButtonClicked;
80+
7381
_dialog = null;
7482
}
7583
}
@@ -80,9 +88,18 @@ void OnClick()
8088
ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);
8189

8290
_dialog = new TimePickerDialog(Context, this, view.Time.Hours, view.Time.Minutes, false);
91+
92+
if (Forms.IsLollipopOrNewer)
93+
_dialog.CancelEvent += OnCancelButtonClicked;
94+
8395
_dialog.Show();
8496
}
8597

98+
void OnCancelButtonClicked(object sender, EventArgs e)
99+
{
100+
Element.Unfocus();
101+
}
102+
86103
void SetTime(TimeSpan time)
87104
{
88105
Control.Text = DateTime.Today.Add(time).ToString(Element.Format);

0 commit comments

Comments
 (0)