2

Hi there I am new to RetroLambda. Right now I'm using it with Runnable, OnClickListener etc. The question is: is it possible to use RetroLambda with classes like onTextChangeListener? For example how to lambda this

etmessage.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (count > 0) {
                okmenubutton.setEnabled(true);
                okmenubutton.getIcon().setAlpha(255);
            } else {
                okmenubutton.setEnabled(false);
                okmenubutton.getIcon().setAlpha(130);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Thank you.

Anton Kazakov
  • 2,740
  • 3
  • 23
  • 34
  • If you are familiar with RxJava, you might want to look at RxBindings library from Jake Wharton. This library allows you to simply subscribe to one of the TextWatchers methods. – Marcel50506 Jul 28 '16 at 08:33

1 Answers1

4

Retrolambda ports Java 8 features to previous JVM versions. When you're using a lambda instead of Runnable it's called automatic SAM conversion, where SAM stands for Single Abstract Method. It means that if you have an interface or an abstract class with a single abstract method you can replace it with a lambda with the same signature. TextWatcher has three abstract methods as you can see and it cannot be replaced with a lambda.

If you want to use lambdas you can define single-method interfaces for each TextWatcher method and implement helper methods that will accept these interfaces, one per method, create a TextWatcher that will delegate invocation to the interface, and add the TextWatcher to TextView.

Michael
  • 53,859
  • 22
  • 133
  • 139