Maui
Maui copied to clipboard
[Bug] MaskedBehavior Cursor jumps to start on unmasked Characters
Description
The cursor jumps to the start when an maskedcharacter is reached.
Link to Reproduction Sample
https://github.com/sh-kd/MaskedBehaviorBug
Steps to Reproduce
- Make an entry with MaskedBehavior that has an maskedcharacter
- Type in the entry till you reach the maskedcharacter
Expected Behavior
That the Cursor goes trough the maskedcharacters
Actual Behavior
The Cursor jumps to the start of the entry. So you have to move the cursor to the end every time an masked character is reached.
Basic Information
-
Version with issue: 1
-
Last known good version:
-
IDE: VS2022
-
Platform Target Frameworks: Android
-
Android Support Library Version:
-
Nuget Packages: MAUI Toolkit
-
Affected Devices: Honeywell RT10A Honeywell TA60A Android 10 API 29. Dont know if it affects other devices
Workaround
no
Reproduction imagery
I cant
Looks like a maui issue - https://github.com/dotnet/maui/issues/6916
i have the same issue here and i dont think it's from maui. My entry was working fine, until i put masked behavior on it. On windows it works fine, but in android, tha issue happens
I'm also experiencing this issue in my project (maui v7.0.49 and toolkit v8.0.0). Is there any update on what's causing this since the linked maui issue, #6916, is now closed?
I've got the same issue, also with maui v7.0.49 and toolkit.maui v3.0.0. I'm using a Samsung Galaxy A53, Samsung Keyboard Settings (English US), in case this might be a device related problem.
Some code changes ITextInput CursorPosition value right after adding a second character after the mask (e.g. MASK 9999-9999-9999-9999 Expected Input 1234-5678-1234-5678 Actual Input 1234-5 (When I click 6 I get result 1623-45)
Seems to be Android only. Works as expected on iOS (both simulator and physical), but getting the issue on a physical Pixel 6.
I got the same problem on Android.
Any workaround for this issue?, I'm having the same problem on Android
I'm seeing the same problem on Android, specifically with the emulator and WSA. Appear to work fine with iOS.
Yeah, I opened an issue on MAUI repo for this, and apparently is fixed on .net-8, but it might not get back-ported to .net-7 😞
Anyone got it to work on .net 7?
Hi @insolito86, on the issue I put a workaround for this case, as I say there is not perfect but works.
Hello @Asfiroth. Wich workaround have you used? could you share?
Thank you.
Hi @candidodmv sure: modifying the Entry handler for android adding this:
On ConnectHandler:
PlatformView.TextChanged += PlatformViewOnTextChanged;
On DisconnectHandler:
PlatformView.TextChanged -= PlatformViewOnTextChanged;
This is the event handler for text changed event
private void PlatformViewOnTextChanged(object sender, TextChangedEventArgs e)
{
if (sender is not AppCompatEditText editText) return;
if (string.IsNullOrWhiteSpace(editText.Text)) return;
var looper = Looper.MyLooper();
if (looper != null)
{
var handler = new Handler(looper);
handler.Post(() =>
{
editText.SetSelection(editText.Text.Length);
});
}else
{
editText.SetSelection(editText.Text.Length);
}
}
As I said, It's not perfect but does the job, any help to make this better is appreciated.
what worked for me was in text changed for the entry, add the following code
(sender as Entry).CursorPosition = (sender as Entry).Text.Length;
Hello @Asfiroth, Thank you for your effort in helping!
But I did preferer to return to v6.0 version v7.0 is pretty unstable yet. At version 6.0 things work as expected.
I still have this issue in the latest .NET 7, is it really fixed in .NET 8 (can't install preview on work machine)?
I am seeing this bug on .NET 8 RC2
I confirm that it does not work on the GA release of .NET 8.0.3 either.
Is this ever going to be fixed?
Either remove the function from the community toolkit or fix it.
@DaveEvans1968 If you have a solution, please submit it via a Pull Request!
I'm seeing this bug in the .NET 8.0.3 . This is a workaround for a problem where the cursor jumps to the start when using certain behavior in the code. To fix it, in the file called Page.xaml.cs, include the following code:
void OnEntryHandlerChanged(object sender, EventArgs e)
{
// Only for Android
if (sender is EntryView entryView)
{
Entry entry = entryView.Entry;
(entry.Handler.PlatformView as AppCompatEditText).TextChanged += textChangedEventHandler;
}
}
void OnEntryHandlerChanging(object sender, HandlerChangingEventArgs e)
{
// Only for Android
if (e.OldHandler != null)
{
(e.OldHandler.PlatformView as AppCompatEditText).TextChanged -= textChangedEventHandler;
}
}
#if ANDROID
private void textChangedEventHandler(object sender, Android.Text.TextChangedEventArgs e)
{
if (!(sender is AppCompatEditText editText) || string.IsNullOrWhiteSpace(editText.Text))
return;
try
{
var looper = Looper.MyLooper();
var handler = new Handler(looper ?? Looper.MainLooper);
handler.Post(() => editText.SetSelection(editText.Text.Length));
}
catch (Exception ex)
{
MobileApp.Instance.LogException(ex);
}
}
#endif
Additionally, in the Page.xaml file, use the following code:
<customElements:EntryView x:Name="phoneEntry"
HandlerChanged="OnEntryHandlerChanged"
HandlerChanging="OnEntryHandlerChanging"
LabelText="Phone"
Keyboard="Numeric"
BindingContext="{Binding PhoneNumber}"
Converter="{StaticResource PhoneNumberConverter}"
Margin="0,20,0,0"/>
I am on .NET 8.0.14 and still having this issue. Please fix it
@kimhongka Would you like to submit a Pull Request?
The only way that is working for me is
I've tried to use Entry Handler way and also adding
protected override void OnTextChanged(string oldValue, string newValue)
{
CursorPosition = Text.Length;
}
with extended entry, but it does not even trigger and both handler and the override method blocks the MaskedBehavior.
I will go with the workaround solution for now.
I am still experiencing this in .NET 8
I am still experiencing this in .NET 8
Me too. Who is experiencing this should put an emoji on the issue, so it gets noticed.
I am still experiencing this in .NET 8
Me too. Who is experiencing this should put an emoji on the issue, so it gets noticed.
Or feel free to submit a PR to fix the issue
Hi Shaun, Wish I had the knowledge for this. Do we know is an issue with the toolkit and not maui itself?