Edittext.Selectionchanged event on Android

Asked

Viewed 42 times

0

I need this event you have in Windows Forms:

private void richTextBox_SelectionChanged(object sender, EventArgs e)
    {

    }

Only for Xamarin, on Android. Note that I’m not using cross-Platform app.

I can use the event EditText_TextChanged thus:

EditText editText;
...
editText = FindViewById<EditText>(Resource.Id.editText);
editText.TextChanged += editText_TextChanged;     

But when I type editText.OnSelectionChanged, get the bug:

Edittext.Onselectionchanged(int, int)' is Inaccessible due to its Protection level.

How do I access this event?

1 answer

1

The Onselectionchanged method is protected, so it can only be accessed by Edittext subclasses.

Create an Edittext subclass by overwriting the method and constructs:

public class MyEditText : EditText {
    public MyEditText (Context context) : base(context) {
    }

    public MyEditText (Context context, IAttributeSet attrs) : base(context, attrs) {
    }

    public MyEditText (Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) {
    }

    public MyEditText (Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) {
    }

    protected MyEditText (IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) {
    }

    protected override void OnSelectionChanged (int selStart, int selEnd) { 
        base.OnSelectionChanged(selStart, selEnd);
        //Sua lógica aqui
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.