Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[Bug] MaskedBehavior Cursor jumps to start on unmasked Characters

Open sh-kd opened this issue 2 years ago • 37 comments

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

  1. Make an entry with MaskedBehavior that has an maskedcharacter
  2. 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

sh-kd avatar Jun 15 '22 12:06 sh-kd

Looks like a maui issue - https://github.com/dotnet/maui/issues/6916

IeuanWalker avatar Jun 15 '22 17:06 IeuanWalker

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

VictorHFerreira avatar Sep 03 '22 14:09 VictorHFerreira

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?

EthanHipps avatar Nov 28 '22 21:11 EthanHipps

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.

EPS-Lac avatar Dec 06 '22 08:12 EPS-Lac

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)

VladislavAntonyuk avatar Jan 15 '23 14:01 VladislavAntonyuk

Seems to be Android only. Works as expected on iOS (both simulator and physical), but getting the issue on a physical Pixel 6.

KieranMaclagan avatar Jan 24 '23 19:01 KieranMaclagan

I got the same problem on Android.

FM1973 avatar Feb 23 '23 08:02 FM1973

Any workaround for this issue?, I'm having the same problem on Android

Asfiroth avatar Feb 26 '23 17:02 Asfiroth

I'm seeing the same problem on Android, specifically with the emulator and WSA. Appear to work fine with iOS.

anotherlab avatar Mar 09 '23 20:03 anotherlab

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 😞

Asfiroth avatar Mar 09 '23 20:03 Asfiroth

Anyone got it to work on .net 7?

insolito86 avatar Mar 16 '23 01:03 insolito86

Hi @insolito86, on the issue I put a workaround for this case, as I say there is not perfect but works.

Asfiroth avatar Mar 16 '23 01:03 Asfiroth

Hello @Asfiroth. Wich workaround have you used? could you share?

Thank you.

candidodmv avatar Apr 20 '23 10:04 candidodmv

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.

Asfiroth avatar Apr 24 '23 20:04 Asfiroth

what worked for me was in text changed for the entry, add the following code

(sender as Entry).CursorPosition = (sender as Entry).Text.Length;

VictorHFerreira avatar Apr 26 '23 02:04 VictorHFerreira

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.

candidodmv avatar Apr 28 '23 11:04 candidodmv

I still have this issue in the latest .NET 7, is it really fixed in .NET 8 (can't install preview on work machine)?

NunoBem avatar Sep 28 '23 22:09 NunoBem

I am seeing this bug on .NET 8 RC2

LarryOlsen avatar Oct 19 '23 23:10 LarryOlsen

I confirm that it does not work on the GA release of .NET 8.0.3 either.

aelmardi avatar Nov 15 '23 16:11 aelmardi

Is this ever going to be fixed?

DaveEvans1968 avatar Dec 18 '23 17:12 DaveEvans1968

Either remove the function from the community toolkit or fix it.

DaveEvans1968 avatar Dec 18 '23 17:12 DaveEvans1968

@DaveEvans1968 If you have a solution, please submit it via a Pull Request!

brminnick avatar Dec 18 '23 17:12 brminnick

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"/>

vikher avatar Jan 04 '24 18:01 vikher

I am on .NET 8.0.14 and still having this issue. Please fix it

kimhongka avatar Apr 03 '24 23:04 kimhongka

@kimhongka Would you like to submit a Pull Request?

brminnick avatar Apr 03 '24 23:04 brminnick

The only way that is working for me is

Screenshot 2024-04-04 at 11 34 32 AM

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.
    

kimhongka avatar Apr 04 '24 00:04 kimhongka

I am still experiencing this in .NET 8

Hackmodford avatar May 06 '24 13:05 Hackmodford

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.

NunoBem avatar May 06 '24 14:05 NunoBem

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

bijington avatar May 06 '24 14:05 bijington

Hi Shaun, Wish I had the knowledge for this. Do we know is an issue with the toolkit and not maui itself?

NunoBem avatar May 06 '24 15:05 NunoBem