简体   繁体   English

AlertDialog EditText 不显示软键盘

[英]AlertDialog EditText doesn't show a soft keyboard

When i click on a TextInputEditText, which is parent of a LinearLayout added programmatically to an AlertDialog , my keyboard doesn't show (tested on multiple devices)当我单击 TextInputEditText(编程方式添加到AlertDialogLinearLayoutAlertDialog ,我的键盘不显示(在多个设备上测试)

First I create a new LinearLayout and add a new Spinner to it.首先,我创建一个新的LinearLayout并向其添加一个新的Spinner After the last item on the spinner is selected, i remove the spinner from the LinearLayout and add a TextInputEditText :选择微调器上的最后一项后,我从LinearLayout删除微调器并添加TextInputEditText

layout.removeAllViews();
layout.addView(input);

When i click on the TextInputEditText it gets focused but no soft keyboard pops up当我点击TextInputEditText它会聚焦没有弹出软键盘


However if i add the TextInputEditText directly as a View to the AlertDialog , the keyboard pops up and gets displayed correctly.但是,如果我将TextInputEditText作为View直接添加到AlertDialog ,则键盘会弹出并正确显示。


My AndroidManifest.xml has no special entrys.我的AndroidManifest.xml没有特殊条目。


My full code:我的完整代码:

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

#. #. Add focus change listener to your TextInputEditText and in onFocusChange() show keyboard using .toggleSoftInput(InputMethodManager.SHOW_FORCED, 0) .将焦点更改侦听器添加到您的TextInputEditText并使用.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)onFocusChange()显示键盘中。

Required to open keyboard:打开键盘需要:

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

#. #. Use below code to hide keyboard when your dialog's button pressed.按下对话框button时,使用以下代码hide键盘。

Required to hide keyboard:需要隐藏键盘:

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

Hope this will help~希望这会有所帮助~

I was having the same problem but any given solution did not work, then i use its parent class (ie Dialog ) instead of AlertDialog .我遇到了同样的问题,但任何给定的解决方案都不起作用,然后我使用它的父类(即Dialog )而不是AlertDialog It worked for me.它对我有用。

If you want to show the keyboard when the dialog opens you can add this after dialog.show() :如果你想在对话框打开时显示键盘,你可以在dialog.show()之后添加:

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

This code works for me.这段代码对我有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM