Pier
Pier

Reputation: 10827

How to setOnEditorActionListener with Kotlin

So I have this Java code:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            doSomething();
            return true;
        }
        return false;
    }
});

I've managed to get this (which I'm not even sure it's the right way):

editText.setOnEditorActionListener() { v, actionId, event ->
      if(actionId == EditorInfo.IME_ACTION_DONE){
          doSomething()
      } else {
      }
}

But I get an error Error:(26, 8) Type mismatch: inferred type is kotlin.Unit but kotlin.Boolean was expected

So how is such event handler written in Kotlin?

Upvotes: 39

Views: 30753

Answers (5)

topcbl
topcbl

Reputation: 849

Kotlin would be great with when keyword instead of using if else

To me, the following code is prettier:

editText.setOnEditorActionListener { v, actionId, event ->
  when(actionId){
      EditorInfo.IME_ACTION_DONE -> {
            doSomething()
            true
      }
      else -> false
  }
}

p/s: The code of @Pier not working because of a expression is required on the right side of lambda. So, we have to use true/false instead of return true/false

Upvotes: 11

Fakhriddin Abdullaev
Fakhriddin Abdullaev

Reputation: 4910

Use this code:

et.setOnEditorActionListener { v, actionId, event ->
            when(actionId){
                EditorInfo.IME_ACTION_DONE -> {
                    Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show()

                true
            }

            EditorInfo.IME_ACTION_NEXT -> {
                Toast.makeText(this, "Next", Toast.LENGTH_SHORT).show()
                true
            }
            else -> false
        }
    }

Upvotes: 0

miensol
miensol

Reputation: 41638

The onEditorAction returns a Boolean while your Kotlin lambda returns Unit. Change it to i.e:

editText.setOnEditorActionListener { v, actionId, event ->
      if(actionId == EditorInfo.IME_ACTION_DONE){
          doSomething()
          true
      } else {
          false
      }
}

The documentation on lambda expressions and anonymous functions is a good read.

Upvotes: 82

Zaydel Eduard
Zaydel Eduard

Reputation: 59

Write simple Kotlin extention for EditText

fun EditText.onAction(action: Int, runAction: () -> Unit) {
    this.setOnEditorActionListener { v, actionId, event ->
        return@setOnEditorActionListener when (actionId) {
            action -> {
                runAction.invoke()
                true
            }
            else -> false
        }
    }
}

and use it

/**
 * use EditorInfo.IME_ACTION_DONE constant
 * or something another from
 * @see android.view.inputmethod.EditorInfo
 */
edit_name.onAction(EditorInfo.IME_ACTION_DONE) {
  // code here
}

Upvotes: 5

kelvincer
kelvincer

Reputation: 6138

You can use another form:

editText.setOnEditorActionListener { _, actionId, _ ->

        if (actionId == EditorInfo.IME_ACTION_DONE) {
            doSomething()
            true
        } else {
            false
        }
}

Upvotes: 0

Related Questions