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

Pau*_*aul 79 android dialog android-softkeyboard android-edittext

所以我有一个似乎常见的问题,就是我的对话框中的EditText在获得焦点时不会显示出来.我已经看过几个解决方法,比如在这个帖子中,这一个这一个(还有更多),但我从来没有见过一个令人满意的解释,为什么这首先发生.

我更倾向于让Android使用自己的EditTexts默认行为,而不是构建我自己的行为,但似乎每个人(在那些线程中)已经接受了Dialogs中EditTexts的默认行为是只给光标而没有键盘.那为什么会这样?

为了记录,这些解决方法似乎都没有对我有用 - 我最接近的是强制键盘出现对话框下面(使用InputMethodManager.toggleSoftKeyboard(*)).我的特殊配置是API15,EditText显示在AlertDialog中ListView的页脚中.设置了EditText android:focusable ="true",onFocusChangeListener 正在接收焦点事件.

编辑:

根据要求,这是我正在使用的特定代码段.我不打算整个布局,但在这个特定的应用程序中,EditText响应于按下对话框上的按钮而出现(类似于动作视图).它包含在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>
Run Code Online (Sandbox Code Playgroud)

构建它的代码将relativeLayout的可见性设置为"Visible"(并隐藏其他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."));
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

使用此配置,当我第一次进入EditText时,onFocusChangedListener会触发,并生成一个总是如下所示的日志:

Focus requested, has focus.
Focus requested, doesn't have focus.
Focus requested, has focus.
Run Code Online (Sandbox Code Playgroud)

键盘显示然后消失,可能是因为我将它切换两次,但即使我确定它保持不动,它也在对话框窗口后面(在灰色区域中),并且没有关闭对话框就无法进入它.

也就是说,我想强调的是,尽管我可能能够解决这个问题,但我主要想找到一个简单的原因,为什么 EditText没有首先触发,为什么这个似乎是如此平常!

Pau*_*aul 180

好了,读了很多之后,我已经想通了,为什么这是一个问题,我也没有需要使用任何变通办法.

问题似乎是(至少在我的情况下),因为你输入文本的地方最初是隐藏的(或嵌套的东西),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);
Run Code Online (Sandbox Code Playgroud)

完成后,EditText就像一个普通的EditText,不需要kludges或变通方法.

  • 当我试图清除标志后,创建它没有工作.它只在我使用它时才有效:Dialog = builder.create(); Dialog.show(); Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); .Dialog.getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); (79认同)
  • SOFT_INPUT_STATE_ VISIBLE对我不起作用.我使用了SOFT_INPUT_STATE_ALWAYS_VISIBLE然后它工作了.如果其他人有同样的问题,就把它放在那里 (19认同)
  • @Bruce太棒了!!! 如果你想要其他人可以看到它,请提交评论:) (2认同)

Gia*_* U. 17

我在自己的应用程序中遇到同样的问题.如果您正在为API级别> = 8进行开发,则可以使用以下代码段:

dialog.setOnShowListener(new OnShowListener() {
    @Override
     public void onShow(DialogInterface dialog) {
         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
         imm.showSoftInput(textEdit, InputMethodManager.SHOW_IMPLICIT);
    }
});
Run Code Online (Sandbox Code Playgroud)

我没有找到较低API级别的解决方案......

顺便说一句:这个代码片段并不总是适用于模拟器.我不知道为什么.

  • 我对这个具体的解决方法并不是很感兴趣,因为我对它为什么不首先工作感兴趣.另外,我实际上并不希望键盘显示对话框,我希望它在EditText获得焦点时显示,就像普通EditText的行为一样. (2认同)

phi*_*s77 17

如果您阅读AlertDialog文档,您会在那里找到:

AlertDialog类需要自动设置护理*WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM*您根据是否在对话框返回从真有什么看法View.onCheckIsTextEditor() .通常,您希望将此集设置为没有文本编辑器的Dialog,以便将其置于当前输入法UI的顶部.您可以通过在调用onCreate后将标志强制为所需模式来修改此行为.

我遇到了你在Dialog里面的ListView中使用EditText提到的问题.我通过使用我自己的FocusableListView覆盖自定义视图类(在我的情况下为ListView)来修复它,只有一个方法被覆盖:

public class FocusableListView extends ListView {

    public FocusableListView(Context context) {
        super(context);
    }

    public FocusableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FocusableListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onCheckIsTextEditor() {
        // this is where the magic happens
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我在布局文件中使用它:

<?xml version="1.0" encoding="UTF-8"?>
<com.myexample.wiget.FocusableListView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:descendantFocusability="beforeDescendants"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

你可以用同样的方式覆盖你的情况下的RelativeLayout,它应该工作.


iLy*_*2IK 7

上面的代码非常有用.但是你必须在"创建"方法之后调用"show"方法(我不知道为什么,但只有这个在我与ListView中的EditText对话框中有效).在方法onCreateDialog中:

@Override
protected Dialog onCreateDialog(int id) {
  switch (id) {
    case YOUR_DIALOG_ID: {
        //...
        AlertDialog a = new AlertDialog.Builder(this)./*
        ... set the properties here
        */
        .create();
        a.show(); //!!! this is very important to call the "show" method
        a.getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        return a;
    }
  //...
  }
  return null;
}
Run Code Online (Sandbox Code Playgroud)


far*_*d_z 7

谢谢!我在嵌入警报对话框片段的Lis​​tView的最后一行中有一个嵌入的TextEdit.我使用你的清除标志的解决方案作为一个可运行的帖子,现在它完美地工作.

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setTitle("My Title");
    m_adapter = new MyAdapter(getContext());
    builder.setAdapter(m_adapter, new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    }); 

    final AlertDialog dialog = builder.create();
    final ListView listView = dialog.getListView();
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

        }
    });

    listView.post(new Runnable() {

        @Override
        public void run() {
            dialog.getWindow().clearFlags(
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                    WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);              
        }
    });
    return dialog;
}
Run Code Online (Sandbox Code Playgroud)


And*_*eis 6

这对我有用.创建AlertDialog.Builder,设置title,positiveButton,negativeButton.这样做之后:

    AlertDialog dialog = builder.create();
    dialog.getWindow().clearFlags( WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    dialog.show();
    editText.requestFocus();
Run Code Online (Sandbox Code Playgroud)

你不需要使用builder.show();.