如何更改按钮的颜色单击后?单击、按钮、如何更改、颜色

2023-09-06 11:46:00 作者:那天夏天的旧少年

我创建按钮的背景颜色,但是当我点击它,它不会显示任何东西。 我需要点击后显示不同颜色的按钮,因为用户需要知道的按钮 点击。照片 我不知道如何做到这一点? 给我的建议。 这里是我的按钮code。

 <按钮机器人:TEXTSIZE =15px的
      机器人:ID =@ + ID /按钮9
      机器人:重力=中心|底
      机器人:文字颜色=@色/ myWhiteColor
      机器人:drawableTop =@可绘制/数学
      机器人:文本=@字符串/ HomePage_Math
      机器人:背景=@色/ myMaroonColor
      机器人:layout_width =54dp
      机器人:layout_height =WRAP_CONTENT>< /按钮>
 

保存在水库解决方案

// XML文件/绘制/ button_bg.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目的android:STATE_ pressed =真
          机器人:颜色=#FFFF0000/> <  - !pressed  - >
    <项目安卓state_focused =真
          机器人:颜色=#FF0000FF/> <! - 集中 - >
    <项目的android:颜色=#FF000000/> <! - 默认 - >
< /选择器>
 

//此布局XML将颜色列表应用到景观:

 <按钮机器人:TEXTSIZE =15px的
      机器人:ID =@ + ID /按钮9
      机器人:重力=中心|底
      机器人:文字颜色=@色/ myWhiteColor
      机器人:drawableTop =@可绘制/数学
      机器人:文本=@字符串/ HomePage_Math
      机器人:背景=@可绘制/ button_bg
      机器人:layout_width =54dp
      机器人:layout_height =WRAP_CONTENT>< /按钮>
 
WPS文档加入的图片怎么改图片背景色

I create button with background color but when i click on it, it's not show anything. I need to show different color on button after click because user need to know button is Click. I don't understand how to do this? Give me suggestion. here is my button code.

<Button android:textSize="15px"
      android:id="@+id/button9" 
      android:gravity="center|bottom" 
      android:textColor="@color/myWhiteColor" 
      android:drawableTop="@drawable/math"
      android:text="@string/HomePage_Math" 
      android:background="@color/myMaroonColor" 
      android:layout_width="54dp" 
      android:layout_height="wrap_content" ></Button>

解决方案

//XML file saved at res/drawable/button_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

//This layout XML will apply the color list to a View:

<Button android:textSize="15px"
      android:id="@+id/button9" 
      android:gravity="center|bottom" 
      android:textColor="@color/myWhiteColor" 
      android:drawableTop="@drawable/math"
      android:text="@string/HomePage_Math" 
      android:background="@drawable/button_bg" 
      android:layout_width="54dp" 
      android:layout_height="wrap_content" ></Button>