我有一个EditText,我正在设置以下属性,以便当用户单击EditText时,我可以在键盘上显示done按钮。
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
当用户点击屏幕键盘上的完成按钮(完成输入),我想改变一个RadioButton状态。
我怎么能跟踪完成按钮时,它是从屏幕键盘击中?
我有一个EditText,我正在设置以下属性,以便当用户单击EditText时,我可以在键盘上显示done按钮。
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
当用户点击屏幕键盘上的完成按钮(完成输入),我想改变一个RadioButton状态。
我怎么能跟踪完成按钮时,它是从屏幕键盘击中?
当前回答
更多关于如何设置OnKeyListener,并让它监听Done按钮的细节。
首先将OnKeyListener添加到类的implements部分。然后添加OnKeyListener接口中定义的函数:
/*
* Respond to soft keyboard events, look for the DONE press on the password field.
*/
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Done pressed! Do something here.
}
// Returning false allows other listeners to react to the press.
return false;
}
给定一个EditText对象:
EditText textField = (EditText)findViewById(R.id.MyEditText);
textField.setOnKeyListener(this);
其他回答
更多关于如何设置OnKeyListener,并让它监听Done按钮的细节。
首先将OnKeyListener添加到类的implements部分。然后添加OnKeyListener接口中定义的函数:
/*
* Respond to soft keyboard events, look for the DONE press on the password field.
*/
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Done pressed! Do something here.
}
// Returning false allows other listeners to react to the press.
return false;
}
给定一个EditText对象:
EditText textField = (EditText)findViewById(R.id.MyEditText);
textField.setOnKeyListener(this);
试试这个,它应该能满足你的需要:
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do here your stuff f
return true;
}
return false;
}
});
<EditText android:imeOptions="actionDone"
android:inputType="text"/>
Java代码如下:
edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Log.i(TAG,"Here you can write the code");
return true;
}
return false;
}
});
芬兰湾的科特林解决方案
在Kotlin中处理它的基本方式是:
edittext.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
callback.invoke()
return@setOnEditorActionListener true
}
false
}
芬兰湾的科特林扩展
使用这个来调用edittext。onDone{/*action*/}在你的主代码。使您的代码更具可读性和可维护性
fun EditText.onDone(callback: () -> Unit) {
setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
callback.invoke()
return@setOnEditorActionListener true
}
false
}
}
不要忘记在编辑文本中添加这些选项
<EditText ...
android:imeOptions="actionDone"
android:inputType="text"/>
如果你需要inputType="textMultiLine"支持,请阅读这篇文章
多亏了奇卡。anddev和Kotlin的Alex Cohn是:
text.setOnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE ||
event?.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) {
doSomething()
true
} else {
false
}
}
这里我检查Enter键,因为它返回EditorInfo。IME_NULL代替IME_ACTION_DONE。
另见Android imeOptions="actionDone"不工作。在EditText中添加android:singleLine="true"。