In my Xamarin app, I want that when value is being entered in an Entry, it automatically finds next Entry field and set focus to it.
I can move to next entry with Next button in keyboard, but I want to set focus on next entry automatically when value is entered (max length is 1 character).
I also tried this code, but it didn't work.
.xml
xmlns:behaviors="clr-namespace:Mobile.App.Behaviors"
<Grid Grid.Column="1">
<Frame CornerRadius="12">
<BoxView Color="#EEEEEE" />
</Frame>
<Entry
x:Name="PasswordOne"
Text="{Binding PasswordOne}"
IsPassword="True"
Keyboard="Numeric"
MaxLength="1"
ReturnType="Next"
Unfocused="Password_Unfocused">
<Entry.Behaviors>
<behaviors:FocusOnReturnBehavior FocusOn="{x:Reference PasswordTwo}" />
</Entry.Behaviors>
</Entry>
</Grid>
<Grid Grid.Column="2">
<Frame CornerRadius="12">
<BoxView Color="#EEEEEE" />
</Frame>
<Entry
x:Name="PasswordTwo"
Text="{Binding PasswordTwo}"
IsPassword="True"
Keyboard="Numeric"
MaxLength="1"
ReturnType="Next"
Unfocused="Password_Unfocused">
<Entry.Behaviors>
<behaviors:FocusOnReturnBehavior FocusOn="{x:Reference PasswordThree}" />
</Entry.Behaviors>
</Entry>
</Grid>
FocusOnReturnBehavior.cs
public class FocusOnReturnBehavior : Behavior<Entry>
{
public VisualElement FocusOn { get; set; }
protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
bindable.Completed += BindableOnCompleted;
}
protected override void OnDetachingFrom(Entry bindable)
{
base.OnDetachingFrom(bindable);
bindable.Completed -= BindableOnCompleted;
}
private void BindableOnCompleted(object sender, EventArgs args)
{
FocusOn?.Focus();
}
}
OnAttachedTo()
is trigger when I'm navigating from page to PasswordPage, but at a time of working withEntry
in PasswordPage, Behavior didn't triggered (breakpoints).. – user14139029