How Show soft keyboard automatically when EditText receives focus

霸气de小男生 提交于 2019-12-08 17:38:20

问题


I want show keyboard when my EditText receives focus. I tried many methods but nothing not helped. I tried: 1.

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

whith different flags.

2.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

  1. <requestFocus />

4.

 editText.setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    editText.post(new Runnable() {
                        @Override
                        public void run() {
                            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                        }
                    });
                }
            });
            editText.requestFocus();

4 method is fork but it bad solution. Thus it is written here Show soft keyboard automatically when EditText receives focus

Before, I used the method 2 and it worked. but now no longer. and I created a blank Project and it does not work, none of the methods

UPDATE:

<style name="Theme.TransparencyDemo" parent="android:Theme.Light.NoTitleBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

回答1:


You can add flags to your activity as well which will show the keyboard automatically

<activity name="package.ActivityName" android:windowSoftInputMode="stateVisible"/>

this is mostly useful if you expect the focus to be applied when the activity launches

Also you can use in Fragment:

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

or in Activity

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);



回答2:


Use WindowManager instead of InputMehtodManager inside onFocusChange listener of edittext, As Its reliable.

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    if (hasFocus) {
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
   } });


来源:https://stackoverflow.com/questions/31779005/how-show-soft-keyboard-automatically-when-edittext-receives-focus

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