警告对话框中的EditText不显示软键盘

3
当我点击一个TextInputEditText时,它是一个LinearLayout的父级,该LinearLayout是以编程方式添加到AlertDialog中的,我的键盘不会显示(在多个设备上测试)。
首先,我创建一个新的LinearLayout,并向其中添加一个新的Spinner。在选择了Spinner上的最后一项后,我将Spinner从LinearLayout中移除并添加一个TextInputEditText:
layout.removeAllViews();
layout.addView(input);

当我点击TextInputEditText时,它会得到焦点,但没有弹出软键盘


然而,如果我直接将TextInputEditText作为View添加到AlertDialog中,键盘会弹出并正确显示。

我的AndroidManifest.xml没有特殊的条目。

完整代码:
private void dialogAddContact() {

    ArrayList<String> mails = new ArrayList<>();

    final LinearLayout layout = new LinearLayout(this);
    final TextInputEditText input = new TextInputEditText(this);
    final Spinner sp = new Spinner(this);

    layout.addView(sp);
    layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

    input.setInputType(InputType.TYPE_CLASS_TEXT);
    input.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    [....]

    final ArrayAdapter<String> adp = new ArrayAdapter<>([....]);

    sp.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    sp.setAdapter(adp);
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Log.d(Tools.LOGTAG, position + " " + totalMails);
            if(position == totalMails - 1){

                /****** Here i remove the spinner and add the input ****/

                layout.removeAllViews();
                layout.addView(input);
            }
        }
        [....]
    });

    final AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setView(layout, 50, 0, 50, 0)
            [....]
            });

    dialog = builder.create();
    dialog.show();
}
4个回答

3

#. 为你的TextInputEditText添加焦点改变监听器,在onFocusChange()中使用.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)显示键盘。

打开键盘所需条件:

final TextInputEditText input = new TextInputEditText(this);

// Keyboard
final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);

// Auto show keyboard
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean isFocused) {

        if (isFocused) {
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        } 
    }
});

#. 当您的对话框中的按钮被按下时,请使用以下代码来隐藏键盘。

需要隐藏键盘:

// Hide keyboard
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);

希望这可以帮到您~


2
我遇到了同样的问题,但是任何给出的解决方案都不起作用,然后我使用它的父类(即Dialog)而不是AlertDialog。对我有用。

0
如果您想在对话框打开时显示键盘,可以在dialog.show()之后添加以下内容:
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

现在键盘已经打开了,但是:http://scshot.deepspace.onl/5cdb1978-249b-4221-9a1f-bd5c389ee33b.png - Sebastian Schneider
但它不能打开吗? - John Sardinha
但它确实在我的AlertDialog的后台打开,它没有被推上来,我也无法点击键盘。 - Sebastian Schneider
2
我建议您为对话框创建一个自定义类,我可以发布另一个带有示例的答案。 - John Sardinha

0

这段代码对我有效。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接