I'm making a dictionary. I want when I press "Enter", the EditText doesn't insert new line and do search instead. How to do it?
I tried OnKey, OnKeyDown, OnEditorAction but it doesn't work. When I press "Enter", EditText 开发者_开发知识库inserts a new line.
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
System.out.println("OnEditor.");
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
System.out.println("OnEditor.");
}
return true;
}
Update: imeOption doesn't works.
Instead of the deprecated singleLine attribute you are supposed to use
android:inputType="text"
to force single line input.
You can also restrict edittext to single line using
android:singleLine="true"
You need to set the android:imeOptions attribute in your XML which will show the appropriate keyboard button. Then you can listen for the onEditorAction call.
add the following line to your edit text
android:imeOptions="actionSend|flagNoEnterAction"
精彩评论