java - 如何在 android 中启用/禁用按钮对象的可绘制对象?

标签 java android xml

我为按钮制作了一个边框,单击该按钮时应启用该边框。

<Button
        android:id="@+id/imageView_red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:src="@drawable/border" />

我的问题是如何启用/禁用此可绘制对象? Java 是否为此提供了解决方案?

我试着看这篇文章,但它似乎没有回答这个问题

Android - border for button

最佳答案

你需要做的就是像这样把选择器放在按钮上

<Button
 android:id="@+id/button1"
 android:background="@drawable/selector_xml_name"
 android:layout_width="200dp"
 android:layout_height="126dp"
 android:text="Hello" />

像这样的drawable

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape>
        <stroke android:width="10dp"/>
    </shape>
</item>
<item android:state_pressed="false">
    <shape>
        <stroke android:width="0dp"/>
    </shape>
</item>

关于java - 如何在 android 中启用/禁用按钮对象的可绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50917163/

相关文章:

html - 在 IntelliJ 的 XML 编辑器中,如何将标签分成多行?

java - 如何在 TextField 中设置文本填充?

java - Wildfly 8.1 - 在没有standalone.xml的情况下设置系统属性

Android Styles 在使用 appcompat-v7 :23. 0.1 时抛出错误

c# - 以特定格式将对象序列化为 XML

java - 如何编辑最初从 .java 源以 XML 创建的 TextView 的内容?

Java NIO 在目录中搜索文件

java - ActionListener - 向 JButtons 的 ArrayList 添加 Action

android - React Native - 为什么我们在 CameraRoll.getPhotos() 中使用 after 属性?

android - 键盘隐藏事件