Search code examples
androidxamarintextchangedusing-directives

Editable using reference for a TextChangedListener


In Xamarin, may I have some help to write some code for a TextChangedListener for an EditText object?

Here is what I have so far:

public class InputTextWatcher
{
    public void afterTextChanged (Editable s)
    {

    }

    public void beforeTextChanged (CharSequence s, int start, int count, int after)
    {

    }

    public void onTextChanged (CharSequence s, int start, int before, int count)
    {

    }
}

This is the error that I am getting:

Error CS0246: The type or namespace name 'Editable' could not be found (are you missing a using directive or an assembly reference?)


Solution

  • You will need to implement ITextWatcher:

    using Android.Text;
    
    public class InputTextWatcher : : Java.Lang.Object, ITextWatcher
    {
        public void AfterTextChanged(IEditable s)
        {
            throw new NotImplementedException ();
        }
    
        public void BeforeTextChanged(Java.Lang.ICharSequence s, int start, int count, int after)
        {
            throw new NotImplementedException ();
        }
    
        public void OnTextChanged(Java.Lang.ICharSequence s, int start, int before, int count)
        {
            throw new NotImplementedException ();
        }
    }
    

    You should also consider using the event handlers instead:

            editText.BeforeTextChanged += HandleBeforeTextChanged;
    
            // or
            editText.TextChanged += (sender, e) => 
            {
    
            };
        }
    
        void HandleBeforeTextChanged (object sender, TextChangedEventArgs e)
        {
    
        }