Question

I have an activity with a numeric EditText sitting above a button.

I have noticed that if you click in the edit text and use the hard keyboard to type some numbers then press the hard enter, that it shifts the focus to the button below the edit text and a QWERTY keyboard appears. How do I stop this from happening?

I have tried to control the enter key with the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activityEnterAmt);

    final EditText editText = (EditText) findViewById(R.id.editTextEnterAmt);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                Log.i("Tag", "Enter pressed or IME Action Submit pressed");
                saveButtonClick(editText);
            }
            return false;
        }
    });
}

But I really want to stop it from opening up the keyboard when you press enter. I don't really understand why it is doing this?

Thanks

Was it helpful?

Solution

I have fixed this issue by closing the soft keyboard when the EditText loses focus:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (v == editText) {
                if (hasFocus) {
                    ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(editText,
                            InputMethodManager.SHOW_FORCED); // open keyboard

                } else {
                    ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            editText.getWindowToken(), 0); // close keyboard
                }
            }
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top