【问题标题】:How to highlight a TextView when you click on it just like when you click on a Button如何在单击 TextView 时像单击按钮一样突出显示它
【发布时间】:2022-01-14 11:12:39
【问题描述】:

默认情况下,当您单击按钮时,它会突出显示。当用户点击 TextView 时如何做同样的事情?

【问题讨论】:

  • Button 默认具有该行为,但 TextView 没有。您必须使用 onClickListener() 手动实现它。

标签: android button textview


【解决方案1】:

您应该能够在 xml 布局文件中进行设置,方法是设置可点击视图并正确设置 foreground 属性。

例如:

...
<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:foreground="?attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
/>
...

请注意,foreground 仅适用于 Android API 版本 23 或更高版本。如果您需要在低于该版本的版本中使用它,请将android:foreground 替换为android:background。不完全相同的行为(波纹效果放置在文本的背景中,而不是属性名称所指示的视图上方的图层中),但它应该可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-27
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2017-09-21
    • 1970-01-01
    • 2015-02-24
    相关资源
    最近更新 更多