AlertDialog EditText不显示软键盘

时间:2016-07-23 13:34:46

标签: android android-layout layout keyboard

当我点击TextInputEditText时, LinearLayout 的父级,以编程方式添加到 AlertDialog,我的键盘没有显示(在多个设备上测试)

首先,我创建一个新的LinearLayout并为其添加新的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 个答案:

答案 0 :(得分:2)

#。使用ini_set('display_errors',1); error_reporting(E_ERROR); set_time_limit(0); ini_set('auto_detect_line_endings', true); ini_set('max_execution_time', 0); include_once('connection.php'); $target ="csv/file_05252017.csv"; $truncateTempTable = mysqli_query($mysqli,"TRUNCATE TABLE temp_location"); if (!$truncateTempTable) { printf("Errormessage: %s\n", $mysqli->error); } $loadDataToTempTableSql = "LOAD DATA LOCAL INFILE '".$target."' INTO TABLE temp_location FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r' IGNORE 1 LINES"; $loadDataToTempTableRes = mysqli_query($mysqli,$loadDataToTempTableSql); if (!$loadDataToTempTableRes) { printf("Errormessage: %s\n", $mysqli->error); } 将焦点更改聆听者添加到TextInputEditTextonFocusChange()显示键盘。

打开键盘需要:

.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键盘。

隐藏键盘需要:

button

希望这会有所帮助〜

答案 1 :(得分:1)

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

答案 2 :(得分:0)

如果要在对话框打开时显示键盘,可以在dialog.show()

之后添加
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

答案 3 :(得分:0)

此代码适用于我。

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