android - onClick() 改变按钮边框颜色而不是改变按钮颜色

标签 android android-button

我是 android 的新手,正在开发一个闹钟应用程序。为此,在这个应用程序中,我有 7 个圆形按钮,每个按钮表示从周日到周六的一周中的几天。

我想做的是当用户点击它时我想改变点击按钮的边框颜色并且按钮文本出现在 textview 上。

然后如果用户随机选择按钮(天),但 TextView 文本(选择的天)应该是有序的。

例如:用户首先选择星期三,然后选择星期日,textview 首先显示星期三,然后显示星期日,这里星期日自动排在第一位,然后是星期三。(如果用户随机选择日期,那么每一天都会有一个特定的位置,textview 将显示所有安排好的日子)

演示.xml

<Button
    android:id="@+id/mbtn_Sun"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/gray_button"
    android:gravity="center_vertical|center_horizontal"
    android:text="Sun"
    android:textAllCaps="false"
    android:textColor="#e20a1f" />

谁能帮我解决这个问题?

最佳答案

我建议改用 Checkbox

下面是一个显示简单实现的示例:

<CheckBox
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:gravity="center"
    android:button="@null"
    android:background="@drawable/checkbox_custom_background" />

checkbox_custom_background.xml 是:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/gray_button" android:state_checked="false"/>
    <item android:drawable="@drawable/green_button" android:state_checked="true"/>
    <item android:drawable="@drawable/gray_button"/>
</selector>

关于android - onClick() 改变按钮边框颜色而不是改变按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50991313/

相关文章:

android - 使用按钮在不同 fragment 中的 webview 中打开网站

android - 按钮文本中的多种字体大小

android - 背景 xml 不工作

android - 更改 Cordova 项目 android 包名称

android - 找不到证书路径的信任 anchor 。在 Android 上使用自签名客户端证书

android - 使用 glide 显示带 ImageView 的动画 gif 文件(android)

android - 我可以在 Google Play 商店中隐藏我的应用程序吗?

java - 针对 Spring Security 的应用程序身份验证

android - 无法为 Android 按钮设置背景资源图像

android - 几个 Textviews 下的按钮