在Android中设置选中/选中的imagebutton

在Android中设置选中/选中的imagebutton,android,android-layout,android-imagebutton,Android,Android Layout,Android Imagebutton,我有一个线性布局,其中包含三个图像按钮。每个图像按钮都有不同的颜色。在初始时间,所有图像按钮均未选中。如果选择了图像按钮(蓝色的V将与图像按钮背景重叠),我想为图像按钮设置选中/选中,并且另一个图像按钮将被取消选中。我如何在安卓系统中做到这一点 这是我的布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

我有一个线性布局,其中包含三个图像按钮。每个图像按钮都有不同的颜色。在初始时间,所有图像按钮均未选中。如果选择了图像按钮(蓝色的
V
将与图像按钮背景重叠),我想为图像按钮设置选中/选中,并且另一个图像按钮将被取消选中。我如何在安卓系统中做到这一点

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="7dp"
    android:layout_centerInParent="true">
    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/color1"
        android:background="@color/colorAccent"/>
    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/color2"
        android:layout_marginLeft="5dp"
        android:background="@color/colorPrimaryDark" />
    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/color3"
        android:layout_marginLeft="5dp"
        android:background="@color/colorPrimary"/>
</LinearLayout>


更新:蓝色
V
表示已检查状态。结果是相似的


我建议对该任务使用复选框

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="7dp"
    android:layout_centerInParent="true">
    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/color1"
        android:button="@drawable/color_selector"
        android:background="@color/accent"/>
    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/color2"
        android:button="@drawable/color_selector"
        android:layout_marginLeft="5dp"
        android:background="@color/primary"
        android:checked="false" />
    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:button="@drawable/color_selector"
        android:id="@+id/color3"
        android:layout_marginLeft="5dp"
        android:background="@color/primary_dark"/>
</LinearLayout>

和color_selector.xml应与所需的选择器一起放入drawable文件夹(仅举一个示例)



希望这会有所帮助。

什么是蓝色v?你的意思是你只能从3个选项中选择1个?没有多选?请详细说明
V
表示检查状态。选中时,它看起来像一个复选框。你是对的。我只需要一次检查,而不是多次检查。您需要对每个图像进行一次检查而不是多次检查的逻辑。单击“太好了”。如果我点击按钮两次,它将类似于我没有点击的状态为什么不使用3个复选框?使用xml定义的自定义选择器?您不需要使用
AppCompatCheckBox
,除非您正在创建自定义
视图
类;在适用的情况下,它将自动使用。来自:“当您在布局中使用
复选框时,将自动使用该类。您只需在编写自定义视图时手动使用该类。”对于延迟,深表歉意。这正是我需要的。您能为
android:bottomLeftRadius
android:bottomrightraius
中的
@dimen/space\u xxsmall
提供一些值吗。。。你可以用你需要的任何东西,我用的是2dp
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true" android:drawable="@drawable/ic_done_white_18dp">
    <shape android:shape="rectangle" >
        <stroke android:color="@color/white" android:width="2dp"/>
        <corners
            android:bottomLeftRadius="@dimen/spacing_xxsmall"
            android:bottomRightRadius="@dimen/spacing_xxsmall"
            android:topLeftRadius="@dimen/spacing_xxsmall"
            android:topRightRadius="@dimen/spacing_xxsmall" />
    </shape>
</item>
<item android:state_checked="false">
    <shape android:shape="rectangle">
        <stroke android:color="@color/white" android:width="1dp"/>
        <corners
            android:bottomLeftRadius="@dimen/spacing_xxsmall"
            android:bottomRightRadius="@dimen/spacing_xxsmall"
            android:topLeftRadius="@dimen/spacing_xxsmall"
            android:topRightRadius="@dimen/spacing_xxsmall" />
    </shape></item>
</selector>