如何显示软键盘时的EditText聚焦键盘、EditText

2023-09-11 12:31:55 作者:゛烟 花 醉 人、

我想自动显示软键盘时的EditText 聚焦(如果设备没有物理键盘),我有两个问题:

I want to automatically show the soft-keyboard when an EditText is focused (if the device does not have a physical keyboard) and I have two problems:

在显示我的的活动,我的的EditText 专注但不显示的键盘,我需要再次点击它以显示键盘(显示我的的活动时,应显示)。

When my Activity is displayed, my EditText is focused but the keyboard is not displayed, I need to click again on it to show the keyboard (it should be displayed when my Activity is displayed).

当我点击键盘上完成,键盘dissmissed但的EditText 保持专注和y不想(因为我的编辑完成)

And when I click done on the keyboard, the keyboard is dissmissed but the EditText stays focused and y don't want (because my edit is done).

要继续,我的问题是有东西更像是在iPhone上:它让我的的EditText 国家(重点/非重点)键盘同步,当然不不present软键盘,如果有一个物理之一。

To resume, my problem is to have something more like on the iPhone: which keep the keyboard sync with my EditText state (focused / not focused) and of course does not present a soft-keyboard if there is a physical one.

推荐答案

要强制软键盘出现,你可以使用

To force the soft keyboard to appear, you can use

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

和有关的EditText 删除焦点,saddly你需要有一个虚拟的查看抓住重点。

And for removing the focus on EditText, saddly you need to have a dummy View to grab focus.

我希望这有助于

要关闭它,你可以使用

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
 
精彩推荐