gpt4 book ai didi

android - 如何处理 ImeOptions 的完成按钮点击?

转载 作者:IT老高 更新时间:2023-10-28 12:54:46 28 4
gpt4 key购买 nike

我有一个 EditText 我正在设置以下属性,以便当用户单击 EditText 时我可以在键盘上显示完成按钮。

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

当用户单击屏幕键盘上的完成按钮(完成输入)时,我想更改 RadioButton 状态。

如何在屏幕键盘点击完成按钮时跟踪它?

Screenshot showing the bottom right 'done' button on the software keyboard

最佳答案

我最终得到了 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;
}
});

更新:上面的代码有时会激活回调两次。相反,我选择了从 Google 聊天客户端获得的以下代码:

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;
}

关于android - 如何处理 ImeOptions 的完成按钮点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2004344/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com