android - 如何让按钮在按下时改变颜色?

标签 android xml button colors

我对 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>

我必须添加什么才能让它在按下时改变颜色?

最佳答案

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

选择器:

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>

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>

关于android - 如何让按钮在按下时改变颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25657200/

相关文章:

android - 为什么 Android Studio 找不到我的资源?

javascript - 使用 AJAX 和 javascript 在表格布局中传递 XML 日期

c# - 如何使用 WebBrowser 控件显示 XML?

javascript - 网站javascript.js :1 Uncaught TypeError: Cannot read property 'addEventListener' of null at sitejavascript. js:1

Android TableLayout - 同一列内的 TextView 和按钮

java - 从调用 Activity 中提取 Intent 中的可分割数据需要很长时间

android - 有没有办法获取所有可用操作系统当前运行的前台应用程序(包)?

android - 如何在处理后手动显示来自 Camera.Preview 回调的帧,无需 Camera.Preview

java - Spring RESTful客户端: root tag exception

android - 在 Activity 之间共享菜单栏