我有一个EditText,我正在设置以下属性,以便当用户单击EditText时,我可以在键盘上显示done按钮。
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
当用户点击屏幕键盘上的完成按钮(完成输入),我想改变一个RadioButton状态。
我怎么能跟踪完成按钮时,它是从屏幕键盘击中?
我有一个EditText,我正在设置以下属性,以便当用户单击EditText时,我可以在键盘上显示done按钮。
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
当用户点击屏幕键盘上的完成按钮(完成输入),我想改变一个RadioButton状态。
我怎么能跟踪完成按钮时,它是从屏幕键盘击中?
当前回答
多亏了奇卡。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"。
其他回答
多亏了奇卡。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"。
我知道这个问题很老了,但我想指出对我有用的方法。
我尝试使用来自Android开发者网站的样例代码(如下所示),但它不起作用。因此,我检查了EditorInfo类,我意识到IME_ACTION_SEND整数值被指定为0x00000004。
来自Android开发者的示例代码:
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextEmail
.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
/* handle action here */
handled = true;
}
return handled;
}
});
因此,我将整数值添加到res/values/integers.xml文件中。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="send">0x00000004</integer>
</resources>
然后,我编辑我的布局文件res/layouts/activity_home.xml,如下所示
<EditText android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeActionId="@integer/send"
android:imeActionLabel="@+string/send_label"
android:imeOptions="actionSend"
android:inputType="textEmailAddress"/>
然后,示例代码工作了。
最后我得到了Roberts和chirags的答案:
((EditText)findViewById(R.id.search_field)).setOnEditorActionListener(
new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// Identifier of the action. This will be either the identifier you supplied,
// or EditorInfo.IME_NULL if being called due to the enter key being pressed.
if (actionId == EditorInfo.IME_ACTION_SEARCH
|| actionId == EditorInfo.IME_ACTION_DONE
|| event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
onSearchAction(v);
return true;
}
// Return true if you have consumed the action, else false.
return false;
}
});
更新: 上面的代码有时会激活回调两次。相反,我选择了以下代码,这是从谷歌聊天客户端:
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// If triggered by an enter key, this is the event; otherwise, this is null.
if (event != null) {
// if shift key is down, then we want to insert the '\n' char in the TextView;
// otherwise, the default action is to send the message.
if (!event.isShiftPressed()) {
if (isPreparedForSending()) {
confirmSendMessageIfNeeded();
}
return true;
}
return false;
}
if (isPreparedForSending()) {
confirmSendMessageIfNeeded();
}
return true;
}
芬兰湾的科特林解决方案
在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"支持,请阅读这篇文章
试试这个,它应该能满足你的需要:
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;
}
});