Android Use Done button on Keyboard to click button

后端 未结 13 1339
-上瘾入骨i
-上瘾入骨i 2020-12-02 06:30

Ok in my app I have a field for the user to input a number. I have the field set to only accept numbers. When the user clicks on the field it brings up the keyboard. On the

13条回答
  •  无人及你
    2020-12-02 07:06

    Kotlin Solution

    The base way to handle the done action in Kotlin is:

    edittext.setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // Call your code here
            true
        }
        false
    }
    

    Kotlin Extension

    Use this to call edittext.onDone {/*action*/} in your main code. Keeps it more readable and maintainable

    edittext.onDone { submitForm() }
    
    fun EditText.onDone(callback: () -> Unit) {
        setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                callback.invoke()
                true
            }
            false
        }
    }
    

    Don't forget to add these options to your edittext

    
    

    If you need inputType="textMultiLine" support, read this post

提交回复
热议问题