作为弹出窗口的一部分创建时,TextInputEditText消失

时间:2018-07-05 10:40:04

标签: android android-layout

我在Android中遇到这个奇怪的问题。当我使用以下代码创建弹出窗口时,TextInputLayout中的TextInputEditText消失。此弹出式窗口是在扩展AppCompatActivity的正常初始为空的活动中创建的。

private void createPopupWindow() {
        // inflate the layout of the popup window
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.popup_window_two, null);

        // create the popup window
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = false; // lets taps outside the popup also dismiss it
        popupWindow = new PopupWindow(popupView, width, height, focusable);

        popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);

        createOkButton();
    }

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/inputGroupNameLayout"
        android:layout_width="368dp"
        android:layout_height="63dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/tan"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.055">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Group Name" />
    </android.support.design.widget.TextInputLayout>
</android.support.constraint.ConstraintLayout>

TextInputEditText中的提示会显示一秒钟,但不会一直停留,并且用户无法输入文本。

为什么会这样?

1 个答案:

答案 0 :(得分:1)

键盘没有显示,因为您将focusable的{​​{1}}参数作为PopupWindow传递了。如果想显示它,则需要通过false

您看到的带有true提示的行为是由于输入自动需要窗口焦点引起的。为了避免这种情况,请添加:

TextInputEditText

android:focusableInTouchMode="true" 的根元素android.support.constraint.ConstraintLayout