如何获取 Android Material TextInputLayout startIcon 视图?

How to get Android Material TextInputLayout startIcon View?

有没有办法获得对 Android Material 库 TextInputLayout startIconView 的引用? TextInputLayout.startIconDrawable 只得到 Drawable,而不是 View。我正在尝试为这个 View 制作动画,所以我需要一个参考。下面是一个示例,我想要一个对心形图标的视图引用。

您可以使用资源标识符名称访问 TextInputLayoutstartIcon,以使用 getIdentifer() of the Resources class.

获取资源整数 ID

TextInputLayout 的标识符名称是 text_input_start_icon

获取startIcon的id:

Kotlin

val startIconViewId = resources.getIdentifier("text_input_start_icon", "id", packageName)

Java

int startIconViewId = getResources().getIdentifier("text_input_start_icon", "id", getPackageName());

并获取视图本身(CheckableImageButton)来制作动画:

Kotlin

val textInputLayout = findViewById<TextInputLayout>(R.id.my_text_input_layout)
val startIcon: CheckableImageButton = textInputLayout.findViewById(startIconViewId)

Java

TextInputLayout textInputLayout = findViewById(R.id.my_text_input_layout);
CheckableImageButton startIcon = textInputLayout.findViewById(startIconViewId);

注意:要使用此过程,您必须在布局中的 TextInputLayout 中设置 app:startIconDrawable

我的测试演示: