问题描述
我有一个EditText场我在哪里设置以下属性,这样我可以显示键盘上的完成按钮,当用户点击文本框。
I am having an edittext field where I am setting the following property so that I can display the done button on the keyboard when user click on the textfield.
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
当用户点击屏幕键盘(输入完成)完成按钮我想换一个单选按钮的状态,我怎么可以跟踪完成按钮当它从屏幕键盘打?
When user clicks the done button on the screen keyboard(finish typing) I want to change a radio button state, how can I track done button when it is hit from screen keyboard ?
推荐答案
我结束了罗伯茨的组合,chirags答案:
I ended up with a combination of Roberts and chirags answers:
((EditText)findViewById(R.id.search_field)).setOnEditorActionListener(
new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
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 false;
}
});
更新:上述code将一些次激活回调的两倍。相反,我选择了以下code,这是我从谷歌聊天客户端有:
Update:The above code would some times activate the callback twice. Instead I've opted for the following code, which I got from the Google chat clients:
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
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;
}
这篇关于安卓的EditText ImeOptions"完成"赛道完成打字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!