Input 如何监听用户的按键输入?
我设法得到一个警报对话框,弹出一个editText来处理用户的输入。当他们在键盘上按Enter键时,我将如何处理提交过程?我想避免使用按钮提交和更改文本。希望我能提供足够的细节,因为我对这一点还很陌生。谢谢你抽出时间 警报对话框:Input 如何监听用户的按键输入?,input,kotlin,keyboard,enter,submit-button,Input,Kotlin,Keyboard,Enter,Submit Button,我设法得到一个警报对话框,弹出一个editText来处理用户的输入。当他们在键盘上按Enter键时,我将如何处理提交过程?我想避免使用按钮提交和更改文本。希望我能提供足够的细节,因为我对这一点还很陌生。谢谢你抽出时间 警报对话框: (1..912).forEach { val id = resources.getIdentifier("Price$it", "id", packageName) val tv = findViewById<TextView&
(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(id)
tv.setOnLongClickListener{
//警报窗口
val alertDialog=alertDialog.Builder(this@MainActivity)
alertDialog.setTitle(“新价格”)
val输入=编辑文本(this@MainActivity)
val lp=LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_父级,
LinearLayout.LayoutParams.MATCH\u父项
)
input.layoutParams=lp
alertDialog.setView(输入).show()
return@setOnLongClickListener真的
}
}
更新:
(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
}
(1..912).forEach{
val id=resources.getIdentifier(“Price$it”、“id”、packageName)
val tv=findViewById(id)
tv.setOnLongClickListener{
//警报窗口
val alertDialog=alertDialog.Builder(this@MainActivity)
alertDialog.setTitle(“新价格”)
val输入=编辑文本(this@MainActivity)
//输入时提交警报
input.setOnKeyListener{v,键代码,事件->
if(event.action==KeyEvent.action\u DOWN&&keyCode==KeyEvent.keyCode\u ENTER){
//输入更改文本
tv.text=input.text
什么时候{
tv.text.startsWith(“-”->tv.setTextColor(Color.RED)
tv.text.startsWith(“+”)->tv.setTextColor(Color.GREEN)
其他->{
tv.text=“\”
tv.setTextColor(Color.DKGRAY)
}
}
//隐藏键盘
//保存价格表
}
假的
}
您可以为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
})
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
}
您可以为
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
})
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
}
您需要为您的
EditText
设置OnEditorActionListener
:
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
})
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\u ACTION\u SEND
或使用xml属性android:imeOptions=“actionSend”
)任何类型的键盘上的任何类型的操作都将调用侦听器。以了解有关操作的更多信息
注意2。我为所有这些逻辑制作了自定义包装,允许我以最简单的方式设置enter键侦听器。请检查
您需要为您的
EditText
设置OnEditorActionListener
:
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
})
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\u ACTION\u SEND
或使用xml属性android:imeOptions=“actionSend”
)任何类型的键盘上的任何类型的操作都将调用侦听器。以了解有关操作的更多信息
注意2。我为所有这些逻辑制作了自定义包装,允许我以最简单的方式设置enter键侦听器。请检查
非常感谢!现在我只需要找到一个关闭键盘功能来添加到它和一个保存状态。非常感谢!现在我只需要找到一个关闭键盘功能来添加到它和一个保存状态。