Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自定义按钮,可在单击时更改文本颜色、背景颜色和绘图_Android_Android Layout_Button - Fatal编程技术网

Android 自定义按钮,可在单击时更改文本颜色、背景颜色和绘图

Android 自定义按钮,可在单击时更改文本颜色、背景颜色和绘图,android,android-layout,button,Android,Android Layout,Button,我在/drawable/中有一个按钮XML文件,可以更改按钮的状态 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/img_pressed" android:state_pressed="true"/&g

我在
/drawable/
中有一个
按钮
XML文件,可以更改按钮的状态

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/img_pressed"
          android:state_pressed="true"/>

    <item android:drawable="@drawable/img_normal"
          android:state_focused="true" />

    <item android:drawable="@drawable/img_normal"/>
</selector>

我可以在这个XML中这样做吗?我不想通过在
setOnClickListener
方法中的Java代码中执行这些额外任务来生成意大利面代码

创建三个不同的xml选择器,并将它们添加到相应的属性中

样本:

TextColor:

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
      android:color="@color/click"/>

<item android:color="@color/normal"/>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
      android:color="@color/buttonBackgroundClick"/>

<item android:color="@android:color/transparent"/>
Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="My button"
   android:textColor="@drawable/textColor"
   android:drawableLeft="@drawable/btn_leftDrawable"
   android:background="@drawable/background"
/>

对,必须为每个任务生成一个XML。谢谢
Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="My button"
   android:textColor="@drawable/textColor"
   android:drawableLeft="@drawable/btn_leftDrawable"
   android:background="@drawable/background"
/>