如何在EditText获得焦点时自动显示软键盘

ip6*_*696 7 android android-softkeyboard android-edittext

当我的EditText获得焦点时,我想显示键盘.我尝试了很多方法,但没有任何帮助.我试过了:1.

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)

不同的旗帜.

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();
Run Code Online (Sandbox Code Playgroud)

4方法是fork但它解决不好.这样写在这里当EditText获得焦点时自动显示软键盘

之前,我使用方法2并且它有效.但现在不再.我创建了一个空白项目,它不起作用,没有一个方法

更新:

<style name="Theme.TransparencyDemo" parent="android:Theme.Light.NoTitleBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

kan*_*idj 8

您还可以为活动添加标记,这将自动显示键盘

<activity name="package.ActivityName" android:windowSoftInputMode="stateVisible"/>
Run Code Online (Sandbox Code Playgroud)

如果您希望在活动启动时应用焦点,这将非常有用

你也可以在片段中使用:

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)

或在活动中

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)