简体   繁体   English

按下按钮后如何使按钮变色?

[英]How do I make the button change color when it's pressed?

I'm a bit new to android programming, so I don't really have an idea of what I'm doing. 我对android编程有点陌生,所以我对自己的工作一无所知。 I want to make a button change color when its pressed. 我想在按下按钮时使按钮改变颜色。 So far, this is what my button looks like: 到目前为止,这是我的按钮的外观:

<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? 我必须添加什么才能使其在按下时变色?

Define the button selector and set it as background of button 定义按钮选择器并将其设置为按钮background

Selector : 选择器:

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>

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>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM