Jetpack compose + TextInputLayout endIconDrawable 未正确显示

时间:2021-06-04 05:42:01

标签: android kotlin android-jetpack-compose

我正在尝试使用 TextInputLayoutTextInputEditText 来实现搜索输入字段,我想说我没有使用 compose,因为它需要一些 UI 自定义,并且 {{1} } 似乎是一个很好的解决方案。 但是,由于某种原因,编辑 TextInputLayoutendIconDrawable 无法正确显示,我将 TextInputEditText 用作 endIconMode。如果 clear_text 中有文字,则应显示 endIcon。

这是代码片段:

XML:

TextInputEditText

撰写:

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/productSearchTextInputLayout"
            android:layout_width="0dp"
            android:layout_height="32dp"
            android:background="@drawable/shape_search_input_product_search"
            app:endIconDrawable="@drawable/ic_icon_clear_text"
            app:endIconMode="clear_text"
            app:hintEnabled="false"
            app:endIconTint="@null"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/productSearchButtonImageView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/searchBarTextView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@null"
                android:clickable="true"
                android:focusable="true"
                android:gravity="center_vertical|start"
                android:hint="@string/navbar_search_products_placeholder"
                android:paddingStart="16dp"
                android:paddingEnd="8dp"
                android:textAlignment="gravity"
                android:textColor="@color/charcoal_grey"
                android:textColorHint="@color/light_grey"
                android:textSize="14sp" />
        </com.google.android.material.textfield.TextInputLayout>

        <ImageView
            android:id="@+id/productSearchButtonImageView"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:background="@drawable/shape_search_button_product_search"
            android:padding="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/productSearchTextInputLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/productSearchTextInputLayout"
            app:layout_constraintTop_toTopOf="@+id/productSearchTextInputLayout"
            app:srcCompat="@drawable/ic_icon_search_main_appbar"
            app:tint="@color/white" />

    </androidx.constraintlayout.widget.ConstraintLayout>

但是,我注意到如果我在 @Composable fun SearchAppBar(name: String, onNameChange: (String) -> Unit, onSearch: (String) -> Unit) { val textState = remember { mutableStateOf(name) } AndroidView( modifier = Modifier.fillMaxSize(), factory = { context -> val searchBarView = LayoutInflater.from(context).inflate(R.layout.search_appbar, null, false) val editText = searchBarView.findViewById<EditText>(R.id.searchBarTextView) editText.doOnTextChanged { text, start, before, count -> onNameChange(text.toString()) textState.value = text.toString() } val productSearchTextInputLayout = searchBarView.findViewById<TextInputLayout>(R.id.productSearchTextInputLayout) searchBarView }, update = { view -> } ) } 部分设置 productSearchTextInputLayout.isEndIconVisible = true,效果很好,但它会在用户开始编辑之前首先显示。

0 个答案:

没有答案