Android:Dialog中的EditText不会拉起软键盘

标签 android dialog android-edittext android-softkeyboard

所以我遇到了一个似乎很常见的问题,即我的对话框中的 EditText 在获得焦点时没有显示出来。我见过几种解决方法,例如 this thread , this onethis one (以及更多),但我从来没有看到一个令人满意的解释为什么首先会发生这种情况。

我更愿意让 android 使用它自己的 EditTexts 的默认行为而不是构建我自己的,但似乎每个人(在这些线程中)都接受了 EditTexts 在对话框中的默认行为是只给出一个光标和没有键盘。为什么会这样?

郑重声明,这些变通办法似乎都不适合我 - 我能做到的最接近的方法是强制键盘出现在对话框下方(使用 InputMethodManager.toggleSoftKeyboard( *))。我的特定配置是 API15,EditText 显示在 AlertDialog 内 ListView 的页脚中。 EditText android:focusable="true"已设置,并且 onFocusChangeListener 正在接收焦点事件。

编辑:

根据要求,这是我正在使用的特定代码段。我不会打扰整个布局,但是在这个特定的应用程序中,EditText 会在按下对话框上的按钮时出现(类似于 an action view )。它包含在默认情况下具有可见性“已消失”的 RelativeLayout:

 <RelativeLayout 
       android:id="@+id/relLay"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerVertical="true"
       android:visibility="gone"
       android:layout_marginTop="5dp"
       android:layout_marginBottom="5dp">

        <ImageButton
            android:id="@+id/cancelBut"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@color/transparent"
            android:src="@drawable/cancelButton" 
            android:layout_margin="5dp"/>

        <ImageButton
            android:id="@+id/okBut"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/cancelBut"
            android:background="@color/transparent"
            android:src="@drawable/okButton"
            android:layout_margin="5dp" />

        <EditText 
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:focusable="true"
            android:layout_toLeftOf="@id/okBut"/>
   </RelativeLayout>

构建它的代码将 relativeLayout 的可见性设置为“可见”(并隐藏其他 UI 元素)。根据我对 EditText 的经验,这 应该 足以在 EditText 获得焦点时拉起键盘。但是,由于某种原因,情况并非如此。我可以设置以下 onFocusChangeListener:

    edit_text.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // For whatever reason we need to request a soft keyboard.
                    InputMethodManager imm = (InputMethodManager)dlg.getWindow().getContext().getSystemService(_Context.INPUT_METHOD_SERVICE);
                    if(hasFocus)
                        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                    Log.v("DialogProblem", "Focus requested, " + (hasFocus?"has focus.":"doesn't have focus."));
                }
            }
        });

使用这个配置,当我第一次进入 EditText 时,onFocusChangedListener 触发,并生成一个看起来总是这样的日志:

Focus requested, has focus.
Focus requested, doesn't have focus.
Focus requested, has focus.

键盘出现然后消失,可能是因为我切换了两次,但即使我确保它保持打开状态,它在对话窗口的后面(在灰色区域中),并且有不关闭对话框就无法访问它。

也就是说,我想强调的是,尽管我可能能够让这个变通办法奏效,但我主要感兴趣找到一个简单的原因为什么 EditText 一开始就没有触发,为什么这似乎很普遍!

最佳答案

好的,所以在阅读了很多之后,我明白了为什么这是一个问题,我确实不需要需要使用任何解决方法。

问题似乎是(至少在我的情况下),因为您输入文本的位置最初是隐藏的(或嵌套的或其他东西),AlertDialog 会自动设置标志 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM (或某种组合和 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE),这样事情就不会触发软输入显示。

我发现解决此问题的方法是在创建对话框后添加以下行:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

完成此操作后,EditText 就像普通的 EditText 一样,不需要任何杂乱或变通方法。

关于Android:Dialog中的EditText不会拉起软键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9102074/

相关文章:

android - 我无法安装 Android SDK 平台工具(无法找到 adb)

android - ProgressDialog 现已弃用

android - 应用程序在后台时未调用 Firebase onMessageReceived

linux - 如何消除运行 "setsid scp"时出现的对话框?

facebook - 无法在 Facebook 提要对话框(直接 URL)上添加带有 url 参数的图片

android - 可以自动完成 EditTextPreference 吗?

java - 附近连接 : Why is the payload file from the Downloads folder null?

android - 调用android对话框而不褪色背景

Android 多个 EditText 与 OnTextChangedListener

Android 在触摸 ListView 时删除软键盘