【问题标题】:How to display soft keyboard after AlertDialog show如何在 AlertDialog 显示后显示软键盘
【发布时间】:2020-09-20 16:15:14
【问题描述】:

我有谷歌这个问题,但没有一个答案对我有用。 这是我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>

这是显示它的代码。

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

以下代码为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>

这个AlertDialog可以正常显示,但我不能显示软键盘

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      这里也有类似情况的问题: when using AlertDialog.Builder with EditText, the Soft Keyboard doesn't pop 或者试试这个: How to show soft-keyboard when edittext is focused

      【讨论】:

      • 在alerDialog.show() 之前使用requestFocus() 和setSoftInputMode(..) 会显示软键盘。此外,您需要指定输入类型,否则将应用默认值。
      猜你喜欢
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      相关资源
      最近更新 更多