Search code examples
androidkotlininline

Inline onFocusChange kotlin


I'm trying to build an inline function for setOnFocusChangeListener

This is what I got so far:

inline fun EditText.onFocusChange(crossinline hasFocus: (Boolean) -> Unit) {
    setOnFocusChangeListener(View.OnFocusChangeListener { view, b -> })
}

And I use it like this

freightTimeOfDay.onFocusChange { doSomething() }

Unfortunately though it gives me no errors, doSomething() is never called.

I'm looking for two things here:

1 - Get a param in there so I can pass it on to doSomething(). For example

freightTimeOfDay.onFocusChange { doSomething(hasFocus) }

2 - Make it work :p, as right now nothing is happening.

Update:

Seems like kotlin already has some type of inline for this

editText.setOnFocusChangeListener { view, b -> doSomething(b) }

However this isn't working for me either, doSomething(hasFocus: Boolean) is never called.

Thanks in advance!


Solution

  • Just to clarify, there isn't really a point in creating an inline method extension. This was my initial objective but I later realized that using:

    editText.setOnFocusChangeListener { view, b -> doSomething(b) }
    

    was possible, it's inline, it's pretty and no extra work is needed