簡體   English   中英

AlertDialog EditText 不顯示軟鍵盤

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

當我單擊 TextInputEditText(編程方式添加到AlertDialogLinearLayoutAlertDialog ,我的鍵盤不顯示(在多個設備上測試)

首先,我創建一個新的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();
}

#. 將焦點更改偵聽器添加到您的TextInputEditText並使用.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)onFocusChange()顯示鍵盤中。

打開鍵盤需要:

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

#. 按下對話框button時,使用以下代碼hide鍵盤。

需要隱藏鍵盤:

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

希望這會有所幫助~

我遇到了同樣的問題,但任何給定的解決方案都不起作用,然后我使用它的父類(即Dialog )而不是AlertDialog 它對我有用。

如果你想在對話框打開時顯示鍵盤,你可以在dialog.show()之后添加:

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

這段代碼對我有用。

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