如何在Android的TextInputLayout中更改setEndIconDrawable的可见性(visible、invisible或gone)?

5

enter image description here

我正在尝试使上图中“FirstName”字段的edit图标(即endIconDrawable)不可见,而保持其他字段的edit图标可见,但无法实现。我该如何做到这一点?
      <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tilFirstName"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_64dp"
                app:endIconDrawable="@drawable/icon_edit"
                app:endIconMode="custom">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etFirstName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusableInTouchMode="false"
                    android:hint="@string/label_fname"
                    android:inputType="textCapWords" />
            </com.google.android.material.textfield.TextInputLayout>

我想要像下面图片中展示的那样:

enter image description here

5个回答

6

如果您想以静态方式进行布局,只需不添加 app:endIconDrawable/app:endIconMode 属性即可:

  <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/tilFirstName"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_64dp">

如果要在程序中删除endIconDrawable,可以使用以下代码:

textInputLayout.setEndIconMode(TextInputLayout.END_ICON_NONE);

为添加endIconDrawable,您可以使用以下方法:
textInputLayout.setEndIconMode(TextInputLayout.END_ICON_CUSTOM);
textInputLayout.setEndIconDrawable(R.drawable.xxxx);

3

我尝试了 @Gabriele Mariotti 的答案。它很好,但有一个问题。我使用了代码来在文本监视器中使结束图标可见或不可见,但它没有按预期工作。然后我使用了 isEndIconVisible 属性来为 textInputLayout 设置为 true 或 false。它按预期工作。


1

2022 KOTLIN

最佳解决方案:

textInputLayout.isEndIconVisible = false

目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

0
将以下步骤添加到此com.google.android.material.textfield.TextInputLayout中。
app:passwordToggleEnabled="true"
app:endIconMode="custom"
app:endIconCheckable="false"
app:endIconDrawable="@drawable/sv_password_hide"

Full code

  <com.google.android.material.textfield.TextInputLayout
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_width="320dp"
                    android:id="@+id/txtlay"
                    android:layout_height="60dp"
                    app:boxBackgroundColor="@color/card_view_bg"
                    app:boxCornerRadiusBottomEnd="10dp"
                    app:boxCornerRadiusBottomStart="10dp"
                    app:boxCornerRadiusTopEnd="10dp"
                    app:boxCornerRadiusTopStart="10dp"
                    app:boxStrokeColor="@color/input_Field_stroke"
                    app:boxStrokeWidth=".5dp"
                    app:passwordToggleEnabled="true"
                    app:endIconMode="custom"
                    app:endIconCheckable="false"
                    app:endIconDrawable="@drawable/sv_password_hide" 
                    app:hintTextColor="@color/input_hint">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/inpCurPass"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:fontFamily="@font/opensans_regular"
                        android:hint="@string/current_password"
                        android:inputType="textPassword"
                        android:maxLines="1"
                        android:padding="10dp"
                        android:singleLine="true"
                        android:textSize="15sp"
                        android:windowSoftInputMode="adjustPan" />
                </com.google.android.material.textfield.TextInputLayout>

在Kotlin文件(活动/片段)中。

mBind.txtlay.setEndIconOnClickListener {
            val s = mBind.txtlay.isEndIconCheckable
            mBind.txtlay.setEndIconMode(TextInputLayout.END_ICON_CUSTOM)
            if (s) {
                mBind.txtlay.isEndIconCheckable = false
                mBind.txtlay.setEndIconDrawable(R.drawable.sv_password_hide)
                mBind.inpCurPass.inputType =
                    InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            } else {
                mBind.txtlay.isEndIconCheckable = true
                mBind.txtlay.setEndIconDrawable(R.drawable.sv_password_show)
                mBind.inpCurPass.inputType =
                    InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
            }
        }


-5

添加这个 android:visibility="gone"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接