Android : EditText losing focus after 'Send'
I am working on a messaging app in which I have an EditText for the user to type his message. I have provided a 'Send' button in keyboard with setImeOptions()
method. However, whenever the user hits the 'Send' Button, the EditText loses focus. (I am doubtful about the word 'focus', but what I mean is the keyboard disappears..)
I find it a bit inconvenient as the user has to click on EditText again to get the keyboard after each send. I have tried editText1.requestFocus()
in the code as follows :
editText1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "send" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
adapter.add(new OneComment(false, editText1.getText().toString()));
editText1.setText("");
editText1.requestFocus();
return true;
}
return false;
}
});
But this doesn't work...please suggest a workaround..thanks :)
你可以尝试这样做
editText1.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_SEND){
editText1.setText("");
editText1.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText1, 0);
}
return true;
}
});
链接地址: http://www.djcxy.com/p/93338.html