当按钮按下时,我如何让它改变颜色?

时间:2022-11-20 23:01:21

I'm a bit new to android programming, so I don't really have an idea of what I'm doing. I want to make a button change color when its pressed. So far, this is what my button looks like:

我对android编程有点陌生,所以我真的不知道我在做什么。我想要做一个按钮,按下它时改变颜色。到目前为止,我的按钮是这样的:

<Button
    android:id="@+id/c1"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#FFFFFF" >

</Button>

What do I have to add to it to make it change color when it's pressed?

我需要添加什么才能让它在按压时改变颜色?

2 个解决方案

#1


5  

Define the button selector and set it as background of button

定义按钮选择器并将其设置为按钮的背景

Selector :

选择器:

button_selector.xml

button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_focused="true" android:drawable="@color/pressed_button_clr"></item>
<item android:state_pressed="true" android:drawable="@color/pressed_button_clr"></item>
<item  android:drawable="@color/default_button_clr"></item>
</selector>

And the xml code is :

xml代码是:

<Button
    android:id="@+id/c1"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/button_selector" >

</Button>

#2


0  

Because you want to define your own background colours the button would appear as if it's not being clicked, if you remove the background colour you defined it would appear to click. In order to define your own background colours you would have to make a custom button

因为你想要定义你自己的背景颜色,这个按钮就会出现,就好像它没有被点击一样,如果你移除你定义的背景颜色,它就会出现点击。为了定义你自己的背景颜色,你需要定制一个按钮

Creating a custom button

创建一个自定义按钮

Create shapes matching the colours you would like

创建与你喜欢的颜色相匹配的形状

Button Clicked shape

按钮点击形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke 
        android:width="1dp" 
        android:color="#505050"/>

    <size android:width="180dp"
        android:height="40dp"/>

    <solid android:color="#505050"/>

</shape>

Button Normal shape

按钮正常形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="1dp"/>

    <size android:width="180dp"
     android:height="40dp"/>

     <solid android:color="#4ddedede"/>

</shape>

When you've created these two shapes, create your button to use them

当您创建了这两个形状后,创建您的按钮来使用它们

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

#1


5  

Define the button selector and set it as background of button

定义按钮选择器并将其设置为按钮的背景

Selector :

选择器:

button_selector.xml

button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_focused="true" android:drawable="@color/pressed_button_clr"></item>
<item android:state_pressed="true" android:drawable="@color/pressed_button_clr"></item>
<item  android:drawable="@color/default_button_clr"></item>
</selector>

And the xml code is :

xml代码是:

<Button
    android:id="@+id/c1"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/button_selector" >

</Button>

#2


0  

Because you want to define your own background colours the button would appear as if it's not being clicked, if you remove the background colour you defined it would appear to click. In order to define your own background colours you would have to make a custom button

因为你想要定义你自己的背景颜色,这个按钮就会出现,就好像它没有被点击一样,如果你移除你定义的背景颜色,它就会出现点击。为了定义你自己的背景颜色,你需要定制一个按钮

Creating a custom button

创建一个自定义按钮

Create shapes matching the colours you would like

创建与你喜欢的颜色相匹配的形状

Button Clicked shape

按钮点击形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke 
        android:width="1dp" 
        android:color="#505050"/>

    <size android:width="180dp"
        android:height="40dp"/>

    <solid android:color="#505050"/>

</shape>

Button Normal shape

按钮正常形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="1dp"/>

    <size android:width="180dp"
     android:height="40dp"/>

     <solid android:color="#4ddedede"/>

</shape>

When you've created these two shapes, create your button to use them

当您创建了这两个形状后,创建您的按钮来使用它们

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