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

时间:2015-08-03 03:27:19

标签: android android-edittext android-softkeyboard

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

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

不同的旗帜。

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

  1. <requestFocus />
  2. 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方法是fork但它解决不好。因此,它写在Show soft keyboard automatically when EditText receives focus

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

    更新:

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

2 个答案:

答案 0 :(得分:8)

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

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

如果您希望在活动启动时应用焦点

,这将非常有用

您也可以在片段

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

活动

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

答案 1 :(得分:1)

在edittext的onFocusChange监听器内使用WindowManager而不是InputMehtodManager,因为它可靠。

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);
    }
   } });