简体   繁体   English

如何在 AlertDialog 显示后显示软键盘

[英]How to display soft keyboard after AlertDialog show

I have Google this proplem, but none of the answers works for me.我有谷歌这个问题,但没有一个答案对我有用。 This is the style of my AlertDialog这是我的AlertDialog的风格

<style name="dialog_full_screen">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:backgroundDimEnabled">true</item>
</style>

This is the code to show it.这是显示它的代码。

  val alertDialog = AlertDialog.Builder(this, R.style.dialog_full_screen).apply {
            setCancelable(true)
        }.create()
        alertDialog.apply {
            val wlp = window!!.attributes
            wlp.gravity = Gravity.BOTTOM
            wlp.flags = wlp.flags and WindowManager.LayoutParams.FLAG_DIM_BEHIND.inv()
            val displayMetrics = DisplayMetrics()
            windowManager.defaultDisplay.getMetrics(displayMetrics)
            wlp.width = displayMetrics.widthPixels
            wlp.height = WindowManager.LayoutParams.WRAP_CONTENT
            window!!.attributes = wlp
        }
        alertDialog.show()

The following code is layout以下代码是layout

<LinearLayout
    android:id="@+id/linear_layout_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    style="@style/CenterInConstraintLayout"
    tools:ignore="MissingConstraints">


<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/index_play_page_icon_comments_null"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/homepage_no_comments"
    android:textColor="@color/color_gray_dark2"
    android:textSize="@dimen/font_size_14"
    />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    android:gravity="center">
    <EditText
        android:id="@+id/edit_text_comment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:maxLength="150"
        android:hint="@string/homepage_hint_comment"
        android:textColorHint="@color/color_gray_dark2"
        android:background="@null"
        android:importantForAutofill="no" />
    <ImageView
        android:id="@+id/image_view_send"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginEnd="30dp"
        />
</LinearLayout>

This AlertDialog can show properly, but i can't show soft keyboard这个AlertDialog可以正常显示,但我不能显示软键盘

alertDialog.window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM) alertDialog.window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)

Give the EditText the type of softkeyboard you want, otherwise default will get applied为 EditText 提供您想要的软键盘类型,否则将应用默认值

// Get the Edit text box to put into the dialog
final EditText input = findViewById(R.id.edit_text_comment)
// set the type of input entered, this would be for entry of a number digit keyboard
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
...
input.requestFocus();
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);


alertDialog.show();

There is also question posted for similar situation here: when using AlertDialog.Builder with EditText, the Soft Keyboard doesn't pop or else try this: How to show soft-keyboard when edittext is focused这里也有类似情况的问题: 当使用带有 EditText 的 AlertDialog.Builder 时,软键盘不会弹出,或者试试这个: How to show soft-keyboard when edittext is focus

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

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