Skip to content

Commit dfeadeb

Browse files
kubafloPureWeen
authored andcommitted
[Android] picker - focus/unfocus events (#28122)
1 parent 57f7f4d commit dfeadeb

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Maui.Controls.Sample.Issues.Issue28121">
5+
<VerticalStackLayout>
6+
<Picker Focused="Picker_Focused"
7+
Unfocused="Picker_Unfocused"
8+
AutomationId="picker">
9+
<Picker.ItemsSource>
10+
<x:Array Type="{x:Type x:String}">
11+
<x:String>Item1</x:String>
12+
<x:String>Item2</x:String>
13+
<x:String>Item3</x:String>
14+
</x:Array>
15+
</Picker.ItemsSource>
16+
</Picker>
17+
<Label x:Name="FocusedLabel"
18+
AutomationId="focusedLabel"
19+
Text="Focused: false"/>
20+
<Label x:Name="UnfocusedLabel"
21+
AutomationId="unfocusedLabel"
22+
Text="Unfocused: false"/>
23+
</VerticalStackLayout>
24+
</ContentPage>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 28121, "Picker Focused/Unfocused Events Do Not Fire", PlatformAffected.Android)]
4+
5+
public partial class Issue28121 : ContentPage
6+
{
7+
public Issue28121()
8+
{
9+
InitializeComponent();
10+
}
11+
12+
void Picker_Focused(object sender, FocusEventArgs e)
13+
{
14+
FocusedLabel.Text = "Focused: true";
15+
}
16+
17+
void Picker_Unfocused(object sender, FocusEventArgs e)
18+
{
19+
UnfocusedLabel.Text = "Unfocused: true";
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#if ANDROID
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
8+
public class Issue28121 : _IssuesUITest
9+
{
10+
11+
public Issue28121(TestDevice testDevice) : base(testDevice)
12+
{
13+
}
14+
15+
public override string Issue => "Picker Focused/Unfocused Events Do Not Fire";
16+
17+
[Test]
18+
[Category(UITestCategories.Entry)]
19+
[Category(UITestCategories.Compatibility)]
20+
public void FocusAndUnfocusEventsShouldFire()
21+
{
22+
App.WaitForElement("picker");
23+
App.Tap("picker");
24+
App.WaitForElement("Item1");
25+
App.Click("Item1");
26+
var focusedLabelText = App.FindElement("focusedLabel").GetText();
27+
var unfocusedLabelText = App.FindElement("unfocusedLabel").GetText();
28+
29+
Assert.That(focusedLabelText, Is.EqualTo("Focused: true"));
30+
Assert.That(unfocusedLabelText, Is.EqualTo("Unfocused: true"));
31+
}
32+
}
33+
#endif

src/Core/src/Handlers/Picker/PickerHandler.Android.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,14 @@ void OnClick(object? sender, EventArgs e)
152152

153153
_dialog.SetCanceledOnTouchOutside(true);
154154

155-
_dialog.DismissEvent += (sender, args) =>
155+
_dialog.DismissEvent += OnDismiss;
156+
157+
void OnDismiss(object? s, EventArgs e)
156158
{
159+
_dialog.DismissEvent -= OnDismiss;
160+
PlatformView?.ClearFocus();
157161
_dialog = null;
158-
};
162+
}
159163

160164
_dialog.Show();
161165
}

0 commit comments

Comments
 (0)