首页 > 解决方案 > 不自动显示软键盘

问题描述

在我的 xml 布局中:

  <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/nameTextInputEditText"
            style="@style/textViewOneLine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/default_margin"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:hint="@string/name"
            android:textColor="@android:color/white"
            android:textColorHint="@android:color/white"
            android:textSize="17sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />

但是当显示活动时,软键盘不显示。我必须单击TextInputEditText以显示键盘。

这无济于事:

   android:focusable="true"
   android:focusableInTouchMode="true"

标签: androidmaterial-design

解决方案


尽管如此,这应该有效。

确保您的父布局不是可聚焦模式。还要确保父布局是相对或线性的。

如果所有这些都失败了,请尝试以下操作:在 setContentView 之后将其添加到 onCreate 中的主要活动中。让我们看看会发生什么。

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

推荐阅读