简体   繁体   English

如何在Android中按下时更改颜色按钮的颜色?

[英]How to change the color of the color button when pressed in Android?

I have some buttons which I set its background color as red, green and blue separately. 我有一些按钮,我将其背景颜色分别设置为红色,绿色和蓝色。 When I press the button, click event is generated, but there is no change in gui for the user to know the button is pressed. 当我按下按钮时,会生成click事件,但gui没有变化,用户知道按下按钮。 Android button's default background grayish color changes to orange and come back to grayish color after releasing the pressed state. Android按钮的默认背景灰色变为橙色,释放按下状态后返回浅灰色。 How to implement this on colored button? 如何在彩色按钮上实现这一点?

That is implemented via a StateListDrawable , represented by selector in XML . 这是通过StateListDrawable实现的,由XML中selector表示。 Reference: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList 参考: http//developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Here is an example of a drawable that will be white by default , black when pressed : 这是一个drawable的例子, 默认情况下是白色, 按下时是黑色:

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

As mentioned by K-Ballo you can use StateListDrawable to implement a view with various different graphics depending on state. 正如K-Ballo所提到的,您可以使用StateListDrawable根据状态实现具有各种不同图形的视图。 In your case Button is the view where two states are Button pressed and button not pressed. 在您的情况下,按钮是两个状态是按下按钮并且未按下按钮的视图。

We need to create a buttonselector.xml file in the drawable folder 我们需要在drawable文件夹中创建一个buttonselector.xml文件

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

    <item android:drawable="@drawable/button_not_pressed" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>

</selector>

Create two separate xml files for the state of the button 为按钮的状态创建两个单独的xml文件

button_not_pressed.xml button_not_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#FFFFFF"
      android:centerColor="#FFFFFF"
      android:endColor="#FFFFFF"
      android:angle="270" />
</shape>

button_pressed.xml button_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#FF0000"
      android:centerColor="#FF0000"
      android:endColor="#FF0000"
      android:angle="270" />
</shape>

You will notice two html color codes #FF0000 & #FFFFFF which represent background color of the button according to the state. 您会注意到两个html颜色代码#FF0000&#FFFFFF,它们根据状态表示按钮的背景颜色。

In you main.xml where you set the style of your custom button 在main.xml中,您可以在其中设置自定义按钮的样式

<Button
        android:id="@+id/customButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/buttonselector"
        android:text="test"
        android:textColor="#000000" />

In you activity class add the following two lines 在您的活动类中添加以下两行

Button customButton = (Button) findViewById(R.id.customButton);
customButton.setBackground(getResources().getDrawable(R.drawable.buttonselector));

Hope it helps 希望能帮助到你

Try out this way: 试试这种方式:

<?xml version="1.0" encoding="utf-8"?>   
<selector xmlns:android="http://schemas.android.com/apk/res/android" >   
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#1E669B" />
            <stroke
                android:width="2dp"
                android:color="#1B5E91" />
            <corners
                android:radius="6dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />            
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#1E669B"
                android:endColor="#1E669B"
                android:angle="270" />
            <stroke
                android:width="4dp"
                android:color="#1B5E91" />
            <corners
                android:radius="7dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

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

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