I've created a very general solution for this:
```
private val DEFAULT_KEYS_TO_HANDLE_AS_DONE_FOR_EDIT_TEXT =
arrayListOf(KeyEvent.KEYCODE_ENTER, KeyEvent.KEYCODE_NUMPAD_ENTER)
private val DEFAULT_ACTIONS_TO_HANDLE_AS_DONE_FOR_EDIT_TEXT =
arrayListOf(
EditorInfo.IME_ACTION_SEND, EditorInfo.IME_ACTION_GO, EditorInfo.IME_ACTION_SEARCH,
EditorInfo.IME_ACTION_DONE
)
@JvmOverloads
fun EditText.setOnDoneListener(
function: Runnable?, onKeyListener: View.OnKeyListener? = null,
onEditorActionListener: TextView.OnEditorActionListener? = null,
actionsToHandle: Collection<Int> = DEFAULT_ACTIONS_TO_HANDLE_AS_DONE_FOR_EDIT_TEXT,
keysToHandle: Collection<Int> = DEFAULT_KEYS_TO_HANDLE_AS_DONE_FOR_EDIT_TEXT
) {
// Log.d("AppLog", "setOnDoneListener caller:${Thread.currentThread().stackTrace[4]}")
setOnEditorActionListener { v, actionId, event ->
if (onEditorActionListener?.onEditorAction(v, actionId, event) == true)
return@setOnEditorActionListener true
if (actionsToHandle.contains(actionId)) {
function?.run()
return@setOnEditorActionListener function != null
}
return@setOnEditorActionListener false
}
setOnKeyListener { v, keyCode, event ->
if (onKeyListener?.onKey(v, keyCode, event) == true)
return@setOnKeyListener true
if (event.action == KeyEvent.ACTION_DOWN && keysToHandle.contains(keyCode)) {
function?.run()
return@setOnKeyListener function != null
}
return@setOnKeyListener false
}
}
```