如何查看 TextInputLayout 中的 endIcon 是否被选中?

问题描述 投票:0回答:3

EndCompoundLayout 中有一个 isEndIconChecked(),但我看不到从 TextInputLayout 访问它的任何方法。

检测当前是否被选中的最佳方法是什么?

在我的例子中,我希望“重复密码”输入仅在未选中“password_toggle”结束模式时可见。

我查看了源代码,现在用作解决方法:

    binding.loginPassword.setEndIconOnClickListener {
        binding.loginPassword.editText?.apply {
            transformationMethod =
                if (transformationMethod is PasswordTransformationMethod) null
                else PasswordTransformationMethod.getInstance()
            binding.repeatPassword.isVisible = transformationMethod != null
        }
    }
android android-textinputlayout
3个回答
1
投票

endIcon.isChecked
给出警告“只能从同一个库组中调用”。但无论如何它都有效。


val endIconViewId = resources.getIdentifier(
    "text_input_end_icon", 
    "id", 
    requireActivity().packageName
)
val endIcon: CheckableImageButton = binding.textInputLayout
    .findViewById(endIconViewId)
var showClearTextPasswordIsChecked = endIcon.isChecked

0
投票

如果你想在 textInput 中添加 passowrd 切换,你可以试试这个

  <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/etPasswordLayout"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.495"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/LogEmail"
                app:layout_constraintVertical_bias="0.028"
                app:passwordToggleEnabled="true"
                android:hint=" ">

                <EditText
                    android:id="@+id/LogPass"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/input_background"
                    android:hint="Password..."
                    android:textColor="@color/white"
                    android:textColorHint="@color/white"
                    android:backgroundTint="@color/f"
                    android:inputType="textPassword"/>
            </com.google.android.material.textfield.TextInputLayout>

0
投票

更新:结合 Vsevolod 的回答,我现在使用:

val endIconViewId = resources.getIdentifier(
    "text_input_end_icon",
    "id",
    requireActivity().packageName
)
val endIcon: CheckableImageButton = binding.newPassword.findViewById(endIconViewId)
endIcon.setOnTouchListener { view, event -> 
   if (event.actionMasked == MotionEvent.ACTION_DOWN)
     binding.repeatPassword.isVisible = endIcon.isChecked
   false
 }

这样,API 内部唯一需要的是 text_input_end_icon 资源,无需更改密码切换的正常行为。

© www.soinside.com 2019 - 2024. All rights reserved.