簡體   English   中英

將 AlertDialog.Builder 與 EditText 一起使用時,軟鍵盤不會彈出

[英]when using AlertDialog.Builder with EditText, the Soft Keyboard doesn't pop

我正在使用AlertDialog.Builder來創建一個輸入框,並將 EditText 作為輸入法。

不幸的是,盡管EditText處於焦點位置,但軟鍵盤不會彈出,除非您再次明確觸摸它。

有沒有辦法強制它彈出?

在 (AlertDialog.Builder).show(); 之后,我嘗試了以下操作; 但無濟於事。

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);

任何人都可以幫忙嗎?

謝謝!!

我做過這樣的事

AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

dialog.show();

我設法像這樣解決它:

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

我發現相同的代碼在平板電腦上正常工作,鍵盤確實彈出,但在手機上卻沒有,所以進一步研究,似乎指向“調整”選項。

我正在用這個,感覺干凈多了。

AlertDialog d = builder.create();
d.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
d.show();

在我的例子中,當顯示對話框時我能夠顯示鍵盤的唯一方法是添加到我的DialogFragment

@Override
public void onResume() {
    super.onResume();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    myEditText.requestFocus();
}

請注意SOFT_INPUT_STATE_ALWAYS_VISIBLE而不是SOFT_INPUT_STATE_VISIBLE

從文檔:

// Visibility state for softInputMode: please always make the soft input 
// area visible when this window receives input focus.
int SOFT_INPUT_STATE_ALWAYS_VISIBLE;

當您調用showDialog()以顯示在onCreateDialog()中使用AlertDialog創建的對話框時

您應該將代碼放在onPrepareDialog()中:

@Override
protected void onPrepareDialog (int id, Dialog dialog, Bundle args)
{
    TextView editText=(TextView) dialog.findViewById(R....);

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
       @Override
       public void onFocusChange(View v, boolean hasFocus) {
         if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
         }
       }
    });
}

這里給出了一個更好的解決方案。

dialog.getWindow().clearFlags(
         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        |WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

沒有解決方法。 EditText的行為符合預期。

在我的例子中,當我在顯示對話框之前(創建它之后)設置它時,SoftInputMode 沒有顯示。 下面的代碼對我有用,我在顯示對話框后設置了 SoftInputMode。

科特林:

val dialog = MaterialAlertDialogBuilder(context) // Other builder code
                .create()
dialog.show()
dialog.window?.apply { // After the window is created, get the SoftInputMode
    clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
    setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}

爪哇:

AlertDialog dialog = MaterialAlertDialogBuilder(getContext()) // Other builder code
                .create();
dialog.show();
Window window = dialog.getWindow();
if(window != null){ // After the window is created, get the SoftInputMode
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

我希望這可以幫助任何與我有同樣問題的人。

Window window = dialog.getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

這已經在這里回答了。 使用 OnFocusChangeListener 對我有用。

試試這個,它對我有用

如果要顯示軟鍵盤:

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(input.getWindowToken(), 0);

如果你想隱藏它:

  InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
final AlertDialog.Builder alert = new AlertDialog.Builder(context);

final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

在調用 AlertDialog.onCreate 后添加 EditText 時會出現此問題。

https://developer.android.com/reference/androidx/appcompat/app/AlertDialog.Builder

AlertDialog 類負責根據對話框中的任何視圖是否從 View.onCheckIsTextEditor() 返回 true 自動為您設置 android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM。

您需要清除 FLAG_ALT_FOCUSABLE_IM 標志。

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 

因為AlertDialog.show是在DialogFragment.onStart中調用的,所以可以在DialogFragment.onStart中插入代碼。

@Override
public void onStart() {
    super.onStart();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

或者,如果您不使用 DialogFragment,則可以使用 Dialog.setOnShowListener。

dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }
});

試試這個,它對我有用

    Window window = dialog.getWindow();
    if (window != null) { // After the window is created, get the SoftInputMode
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

我找到了一個簡單可靠的解決方案,如果您有一個復雜的布局,而可編輯字段不在根目錄中,只需將隱藏的 EditText 放在對話框布局的根目錄中,

<!-- Just to trick AlertDialog to not hide soft keyboard -->
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" />

這基本上是為了欺騙 compat/androidx 的這一部分

我曾經使用上面的onResume解決方案,但我無法使用更簡單的AlertDialog.Builder() API 來刪除AppCompatDialogFragment的使用,但現在我可以簡單地使用更簡單的 API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM