问题
I have an Edittext for searching on Listview.
I have set imeOption
to 'IME_ACTION_SEARCH' so that soft-keyboard will show search key on it.
The problem is that when I tap on search key on keyboard if edittext contains no text in it, the search key changes to 'Done' instead of dismissing the keyboard.
If the Edittext contains some text in it the search key works well.
回答1:
In your XML layout file you can set
<EditText android:imeOptions="actionSearch" />
Or in your Java source file you could do
yourTextField.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
Then you can override the listener for the search event.
yourTextField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//if textfield value is empty then close keyboard.
//else call your search function.
}
}
});
回答2:
you should do this to your edittext in xml
android:imeOptions="actionSearch|actionSearch"
this will show you search button always!
回答3:
I found the problem behind this. It was happening because the OnEditorAction
Listener was not properly set on Edittext. Since listener was not properly set call was not reaching onEditorAction()
method.
来源:https://stackoverflow.com/questions/18894877/imeaction-search-is-changed-to-done-after-tapping-on-it