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

当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。

继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。


当前回答

你可以试着强迫软键盘出现,这对我来说是有效的:

...
dialog.show();
input.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

其他回答

这很疯狂,但确实有效

fun showKeyboard(view: View) {
    try {
        InputMethodManager::class.java.getMethod(
                "showSoftInputUnchecked",
                Int::class.javaPrimitiveType,
                ResultReceiver::class.java
        ).apply {
            isAccessible = true
            invoke(view.context.inputMethodManager, 0, null)
        }
    }
    catch (e: Exception) {
       e.printStackTrace()
    }
}

没有一个答案对我有用。这里有一个简单的方法。

searchEditText.setVisibility(View.VISIBLE);
                final Handler handler=new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        searchEditText.requestFocus();
                    }
                }, 400);

只是将requestFocus()方法延迟了400ms。

如果EditText是在回收器或ListView和/或这有禁用状态使用下面的代码。

public static void showKeyboardByFocus(final View view)
    {
        view.requestFocus();

        InputMethodManager keyboard = SystemMaster.getInputMethodManager();
        keyboard.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);

        Runnable re = new Runnable()
        {
            @Override
            public void run()
            {
                view.setEnabled(true);
                view.requestFocus();
            }
        };

        Handler h = new Handler(Looper.getMainLooper());
        h.postDelayed(re, 360);
    }

信不信由你,当我发现活动动画可以禁用软键盘时,我的软键盘问题得到了解决。当你用

i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

and

overridePendingTransition(0, 0);

它可以隐藏软键盘,没有办法显示它。

在添加了这些线条后,对我来说很有用。

globalSearchBarMainActivity.setEnabled(true);
globalSearchBarMainActivity.requestFocus();

因为我的自动完整文本视图方法被隐藏,即可见:消失

所以需要加上上面两行

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(globalSearchBarMainActivity, InputMethodManager.SHOW_IMPLICIT);