简体   繁体   English

聚焦EditText时键盘不显示

[英]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. 您可以通过将EditText包含在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... 但这并不比众所周知的解决方法(手动启用软件键盘)令人满意,因为我们不知道为什么在这种情况下它可以更好地工作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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