Show software keyboard without EditText

北战南征 提交于 2019-12-01 02:40:13

Even if this question was asked almost a year ago it didn't have an accepted and fully helpful answer and since I ran into the same problem myself I though I'd share my solution:

As Vikram pointed out this is the way to show the soft input:

InputMethodManager im = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(myView, InputMethodManager.SHOW_FORCED);

BUT you must also set your view as focusable and focusable in touch mode:

myView.setFocusable(true);
myView.setFocusableInTouchMode(true);

or in your view XML:

android:focusable = "true"
android:focusableInTouchMode = "true"

You can force the Softkeyboard to be shown by using:

InputMethodManager im = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(myView, InputMethodManager.SHOW_FORCED);

and to hide:

((InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(findViewById(R.id.YOUR_VIEW).getWindowToken(), 0);

Actually, you can always show a soft keyboard input from manifest. add this line to each activity you want to show soft keyboard:

android:windowSoftInputMode="stateAlwaysVisible"

Soft keyboard will show up no matter if there's no edittext in the view. example:

<activity android:name=".ChatActivity"
    android:windowSoftInputMode="stateAlwaysVisible">

</activity>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!