如何监听用户的按键输入?

How to listen for Key input from User?

我设法让一个警告对话框弹出,其中包含一个 editText 来处理来自用户的输入。当他们按键盘上的 Enter 键时,我将如何处理提交过程?我想避免使用按钮来提交和更改文本。希望我提供了足够的细节,因为我对此还很陌生。谢谢你的时间。

Phone App Pic

警报对话框:

(1..912).forEach {
        val id = resources.getIdentifier("Price$it", "id", packageName)
        val tv = findViewById<TextView>(id)
        tv.setOnLongClickListener {

            //Alert Window
            val alertDialog = AlertDialog.Builder(this@MainActivity)
            alertDialog.setTitle("NEW PRICE")
            val input = EditText(this@MainActivity)
            val lp = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
            )
            input.layoutParams = lp
            alertDialog.setView(input).show()
            return@setOnLongClickListener true

        }
    }

更新:

(1..912).forEach {
        val id = resources.getIdentifier("Price$it", "id", packageName)
        val tv = findViewById<TextView>(id)
        tv.setOnLongClickListener {

            //Alert Window
            val alertDialog = AlertDialog.Builder(this@MainActivity)
            alertDialog.setTitle("NEW PRICE")
            val input = EditText(this@MainActivity)
            //Alert Submit on Enter
            input.setOnKeyListener { v, keyCode, event ->
                if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                    // Input changes text
                    tv.text = input.text
                    when {
                        tv.text.startsWith("-") -> tv.setTextColor(Color.RED)
                        tv.text.startsWith("+") -> tv.setTextColor(Color.GREEN)
                    else -> {
                        tv.text = "_"
                        tv.setTextColor(Color.DKGRAY)
                    }
                    }
                    // Hide Keyboard
                    // Save Price Table
                }
                false
            }

您可以为 EditText 设置自定义 OnKeyListener:

val input = EditText(this@MainActivity)
input.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
    if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
        // your code here
        true
    }
    false
})

您需要为您的 EditText 设置 OnEditorActionListener:

val input = EditText(this@MainActivity)
input.setOnEditorActionListener { _, actionId, event ->
    // If triggered by an enter key, this is the event; otherwise, this is null.
    // if shift key is down, then we want to insert the '\n' char in the TextView;
    if (event == null || event.isShiftPressed) return@setOnEditorActionListener false
    // TODO: your code goes here
    return@setOnEditorActionListener true
}

在此示例中,我还检查了是否未按下 shift。它适用于具有任何类型键盘的所有设备。

注 1。我们这里不需要 actionId,但是你仍然可以为键盘设置不同的操作(使用 input.imeOptions = EditorInfo.IME_ACTION_SEND 或使用 xml 属性 android:imeOptions="actionSend")并且监听器将被调用在任何类型的键盘上进行友好操作。 Read Android documentation 了解有关操作的更多信息。

注2。我为所有这些逻辑制作了自定义包装器,允许我以最简单的方式设置回车键监听器。查看 this gist

editText.setOnEnterActionListener {
    Toast.makeText(context, "Click", Toast.LENGTH_SHORT).show()
}