【问题标题】:How to get Android Material TextInputLayout startIcon View?如何获取 Android Material TextInputLayout startIcon View?
【发布时间】:2021-01-02 02:45:44
【问题描述】:

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

【问题讨论】:

  • 您可以随时进行自定义布局。请分享一些图片,以便我了解您想要实现的目标
  • 当然,我加了一个例子

标签: android kotlin material-components-android


【解决方案1】:

您可以使用TextInputLayout 的资源标识符名称访问startIcon,以使用Resources 类的getIdentifer() 获取资源整数ID。

TextInputLayout 的标识符名称是 text_input_start_icon

获取startIcon的id:

科特林

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

Java

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

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

科特林

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);

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

我的测试演示:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-21
    • 2016-11-20
    • 2021-04-04
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    相关资源
    最近更新 更多