問題描述
我有一個 EditText
我正在設置以下屬性,以便當用戶單擊 EditText 時我可以在鍵盤上顯示完成按鈕.
I am having an EditText
where I am setting the following property so that I can display the done button on the keyboard when user click on the EditText.
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
當用戶單擊屏幕鍵盤上的完成按鈕(完成輸入)時,我想更改 RadioButton
狀態.
When user clicks the done button on the screen keyboard (finished typing) I want to change a RadioButton
state.
如何在屏幕鍵盤點擊完成按鈕時跟蹤它?
How can I track done button when it is hit from screen keyboard?
推薦答案
我最終得到了 Roberts 和 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) {
// 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 聊天客戶端獲得的以下代碼:
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 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 '
' 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;
}
這篇關于如何處理 ImeOptions 的完成按鈕點擊?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!