0

I want an ediText field in my app to respond to onTextChange events so a method is called when the text changes. I am using dot42 to make the app but could only find tutorials available in Java.

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
desolator
  • 27
  • 5

2 Answers2

4

This would be a minimal implementation. I hope it helps you see a pattern in the difference between Java and C# - it is really trivial.

   [Activity]
   public class MainActivity : Activity, Android.Text.ITextWatcher
   {
      protected override void OnCreate(Bundle savedInstance)
      {
         base.OnCreate(savedInstance);
         SetContentView(R.Layouts.MainLayout);

         EditText editText = FindViewById<EditText>(R.Ids.status);
         editText.AddTextChangedListener(this);
      }

      public void AfterTextChanged(Android.Text.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();
      }
   }

The interface implementation is generated by Visual Studio.

Frank Rem
  • 3,632
  • 2
  • 25
  • 37
1

In Java you'd do it by implementing a TextWatcher interface. Dot42 documentation says that

AddTextChangedListener(ITextWatcher watcher)

Adds a TextWatcher to the list of those whose methods are called whenever this TextView's text changes.` 

So implement ITextWatcher, do something in AfterTextChanged and you're OK.

Axarydax
  • 16,353
  • 21
  • 92
  • 151
  • I read the documentation but didn't help. Please share sample code if you have . – desolator Nov 14 '13 at 13:06
  • I don't have any sample code. Did you try to do as I suggested? Did you call myTextView.AddTextChangedListener with the custom ITextWatcher? – Axarydax Nov 14 '13 at 13:30