Monday 10 December 2012

Android - Disabling enter key press for a text editor

//A bug where hitting enter after entering text in a EditText box would clear the box
//the code below prevents this
//disable enter key press
EditText txtEdit = (EditText)view.findViewById(R.id.text_input);
txtEdit.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(v.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
return true;
}
return false;
}
});

No comments:

Post a Comment