简体   繁体   中英

Keyboard doesn't show up when EditText is in focus

I want to do the following. User should be able to input one letter (only letter) from standard keyboard (hardware or software). If he is typing another letter, then the previous letter should be replaced with this one. So only the current letter should be displayed. User should be able to dismiss this dialog and get back to activity. And if he clicked "done" button in keyboard the activity should know what letter he entered.

So I thought about alert dialog and edit text with some extensions to display only current char. This is easy.

However, and this gets me mad already, although edit text is in focus the keyboard does not appear on the screen until edit text is clicked. Why is that so? It should be, should it not?

I won't take something like the following for the answer, because I shouldn't have to do it manually. It should be automatic. Besides, I'm not sure how this would work with a hardware keyboard.

InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(YourEditText.getWindowToken(), 0);

I want to know why exactly the keyboard is not shown after edit text has focus? And what should I do to get this functionality without manually enabling and disabling software keyboard.

Use this.

EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

For your information , see this related question: Why the soft keyboard shows or not when an activity starts? . You can read it as an alternative by enclosing your EditText into a ScrollView.

But it's not more satisfying than the well known workaround (manually enabling the software keyboard) because we don't understand why it works better in this case...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM