Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理EditText上的Enter键(Kotlin,Android)_Android_Android Edittext_Kotlin - Fatal编程技术网

处理EditText上的Enter键(Kotlin,Android)

处理EditText上的Enter键(Kotlin,Android),android,android-edittext,kotlin,Android,Android Edittext,Kotlin,如何在Android Kotlin语言中处理EditText上的Enter键?下面是解决上述问题的最简单方法 editText.setOnKeyListener(View.OnKeyListener { v, keyCode, event -> if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {

如何在Android Kotlin语言中处理EditText上的Enter键?

下面是解决上述问题的最简单方法

    editText.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
                    //Perform Code 
                    return@OnKeyListener true
                }
                false
            })

我使用when表达式检查是否单击了enter按钮

edittext.setOnKeyListener { v, keyCode, event ->
        
        when {

            //Check if it is the Enter-Key,      Check if the Enter Key was pressed down
            ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.action == KeyEvent.ACTION_DOWN)) -> {
                
                
                //perform an action here e.g. a send message button click
                sendButton.performClick()

                //return true
                return@setOnKeyListener true
            }
            else -> false
        }


    }

第1步[重要] 在XML中指定两个属性:

  • android:imeOptions
    以向用户显示要按下的正确按钮
  • android:inputType
    告诉用户需要输入什么文本、数字文本等
  • 步骤2在Kotlin文件中添加:

    yourEditText.setOnEditorActionListener { _, keyCode, event ->
            if (((event?.action ?: -1) == KeyEvent.ACTION_DOWN)
              || keyCode == EditorInfo.IME_PUT_THE_ACTION_YOU_HAVE_SET_UP_IN_STEP_1) {
              
                 // Your code here
    
                return@setOnEditorActionListener true
            }
            return@setOnEditorActionListener false
    }
    
    此代码处理硬件和软件
    enter
    键。我之所以选择
    setOnEditorActionListener
    ,是因为它能更好地处理此操作。根据文件:

       /**
         * Set a special listener to be called when an action is performed
         * on the text view.  This will be called when the enter key is pressed,
         * or when an action supplied to the IME is selected by the user.  Setting
         * this means that the normal hard key event will not insert a newline
         * into the text view, even if it is multi-line; holding down the ALT
         * modifier will, however, allow the user to insert a newline character.
         */
    

    确保检查
    操作是向下还是向上,以免重复
    if(keyCode==KeyEvent.keyCode\u ENTER&&event.action==KeyEvent.action\u UP)
    感谢您的反馈。我使用了return@OnKeyListener执行操作后隐藏键盘为false