That looks like a bug to me - I would definitely submit a support request to HipByte, if you haven't already.
TextWatcher is an interface, just like
View.OnClickListener so you should be able to pass in any class that implements
TextWatcher's methods to a method that expects a
TextWatcher, just like we do with
setOnClickListener. I can't see why there would be any difference between the two, so it seems like it must be a bug.
Your first approach definitely would not work because
TextWatcher is an interface, and you can't create a concrete subclass of an interface. In JavaLand, you would either create it as an anonymous inner class (as your StackOverflow example did), or add the
implements clause to your class declaration. Unfortunately, neither of these options are available to us in Ruby.
There is one ugly workaround that seems to work, however. There's an Android class called
PhoneNumberFormattingTextWatcher which implements
TextWatcher and doesn't appear to do much else. Because it's a concrete class, not an interface, we can subclass it, and that will keep Java's type system happy. You just need to make sure you override all of the methods so you don't pick up any behavior you don't want.
I tried the following code in a new project and it seemed to work:
class MainActivity < Android::App::Activity
view = Android::Widget::EditText.new(self)
view.text = "Hello World!"
self.contentView = view
class MyTextWatcher < Android::Telephony::PhoneNumberFormattingTextWatcher
puts "in afterTextChanged"
def beforeTextChanged(str, start, count, after)
puts "in beforeTextChanged"
def onTextChanged(str, start, before, count)
puts "in onTextChanged"
I hope that works for you until we get a fix from HipByte - good luck!