Android各控件实现背景选中效果总结

前言

selector实现背景选中替换效果,很容易的一个东西,但每次用到都很容易把各控件的使用方法记混,索性写一篇总结方便下次查找。本篇总结的控件包括CheckBox、ImageButton、RadioButton、Button、ImageView。推荐使用CheckBox,下面让我们来分析各控件的使用方法以及为什么推荐使用CheckBox
效果图如下
这里写图片描述

CheckBox

使用步骤

1、创建checkbox_bg.xml文件

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

2、布局文件里添加

<CheckBox
        android:id="@+id/cb_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_cb"
        android:layout_marginTop="10dp"
        android:button="@drawable/checkbox_bg" />
分析

操作简单、方便、图片无变形,推荐使用

ImageButton

使用步骤

1、创建imagebutton_bg.xml文件

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/open" android:state_pressed="true"/>
    <item android:drawable="@mipmap/open" android:state_selected="true"/>
    <item android:drawable="@mipmap/close" android:state_enabled="true"/>
</selector>

2、布局里添加

<ImageButton
        android:id="@+id/ib_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:layout_below="@id/tv_ib"
        android:layout_marginTop="10dp"
        android:src="@drawable/imagebutton_bg"/>

3、activity里添加监听判断

ibButton = (ImageButton) findViewById(R.id.ib_button);
ibButton.setOnClickListener(this);
boolean ibFlag = false;
@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.ib_button:
            if (!ibFlag){
                ibFlag = true;
                ibButton.setSelected(true);
            } else {
                ibFlag = false;
                ibButton.setSelected(false);
            }
            break;
    }
 }
分析

需手动添加判断,设置其是否有选中效果,如不加判断则只有点击效果,图片无变形,可使用

RadioButton

使用步骤

1、创建radiobutton_bg.xml文件

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/open" android:state_pressed="true" />
    <item android:drawable="@mipmap/open" android:state_focused="true" />
    <item android:drawable="@mipmap/open" android:state_checked="true" />
    <item android:drawable="@mipmap/close"/>
</selector>

2、在布局文件里添加

<RadioGroup
        android:id="@+id/rb_group"
        android:layout_below="@id/tv_rg"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/rb_button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:background="@drawable/radiobutton_bg"/>

        <RadioButton
            android:id="@+id/rb_button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:button="@null"
            android:background="@drawable/radiobutton_bg"/>

    </RadioGroup>
分析

RadioButton如果是单个使用只有选中效果,无法取消,如果想要有选中取消效果,必须和RadioGroup结合使用,且同时只能选中一个,图片稍微有变形,所以如果是单选选中效果不推荐使用

Button

使用步骤

1、创建button_bg.xml文件

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/open" android:state_pressed="true"/>
    <item android:drawable="@mipmap/open" android:state_selected="true"/>
    <item android:drawable="@mipmap/open" android:state_focused="true"/>
    <item android:drawable="@mipmap/close" />

</selector>

2、布局文件里添加

<Button
        android:id="@+id/b_button"
        android:layout_width="60dp"
        android:layout_height="16dp"
        android:layout_below="@id/tv_b"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_bg"/>

3、activity里添加监听判断

bButton = (Button) findViewById(R.id.b_button);
bButton.setOnClickListener(this);
boolean bFlag = false;
@Override
public void onClick(View v) {
        switch (v.getId()){
        case R.id.b_button:
            if (!bFlag){
                bFlag = true;
                bButton.setSelected(true);
            } else {
                bFlag = false;
                bButton.setSelected(false);
            }
            break;
      }
}
分析

图片变形严重,需要设置固定宽高,需手动添加判断,设置其是否有选中效果,如不加判断则只有点击效果,可实现效果,但不推荐使用

ImageView

使用步骤

1、创建imageview_bg.xml文件

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/open" android:state_pressed="true"/>
    <item android:drawable="@mipmap/open" android:state_selected="true"/>
    <item android:drawable="@mipmap/open" android:state_focused="true"/>
    <item android:drawable="@mipmap/close" />
</selector>

2、布局文件里添加

<ImageView
        android:id="@+id/iv_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_iv"
        android:layout_marginTop="10dp"
        android:clickable="true"
        android:background="@drawable/imageview_bg"/>

3、activity里添加

ivButton = (ImageView) findViewById(R.id.iv_button);
ivButton.setOnClickListener(this);
boolean ivFlag = false;
@Override
public void onClick(View v) {
        switch (v.getId()){
        case R.id.iv_button:
            if (!ivFlag){
                ivFlag = true;
                ivButton.setSelected(true);
            } else {
                ivFlag = false;
                ivButton.setSelected(false);
            }
            break;
        }

}
分析

需手动添加判断,设置其是否有选中效果,如不加判断则只有点击效果,图片无变形,可使用

总结

通过以上的对比,CheckBox是最简洁、方便、效果不错的做法,推荐使用~

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,可以使用以下方法来设置控件选中属性: 1. 在布局文件中设置 android:background 属性来设置控件选中时的背景。 2. 在代码中使用 setSelect() 方法来设置控件选中状态。 下面是一个示例代码,演示如何设置控件选中属性: ``` <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:background="@drawable/bg_selector" /> ``` 在这个示例中,使用了 @drawable/bg_selector 来设置 TextView 控件选中背景。这里的 bg_selector 是一个 XML 文件,定义了控件选中状态和非选中状态下的背景: ``` <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/colorAccent" android:state_selected="true" /> <item android:drawable="@color/colorPrimary" /> </selector> ``` 在这个示例中,当 TextView 控件选中时,背景颜色为 colorAccent;非选中时,背景颜色为 colorPrimary。 在代码中,使用 setSelect() 方法可以设置控件选中状态: ``` TextView textView = findViewById(R.id.textView); textView.setSelected(true); // 设置选中状态为 true textView.setSelected(false); // 设置选中状态为 false ``` 在这个示例中,使用 setSelected() 方法来设置 TextView 控件选中状态。当 setSelected(true) 时,TextView 控件选中;当 setSelected(false) 时,TextView 控件取消选中状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值