Android实现自由单选、复选按钮效果+样式美化[通俗易懂]

Android实现自由单选、复选按钮效果+样式美化[通俗易懂]背景Android开发中会遇到将单选按钮排布在多行的情况,一般只能通过自定义控件的形式,绘制单选按钮,网络上也有很多这样的文章,但一般情况下自定义的控件在界面美观性、效果方面稍有欠缺。因此,我们打算用CheckBox+LinearLayout来实现一种多行单选按钮组的效果。效果如下:思路Android中要实现单选按钮要用到RadioGroup+RadioButton的布局结…

大家好,又见面了,我是你们的朋友全栈君。

背景

Android开发中会遇到将单选按钮排布在多行的情况,一般只能通过自定义控件的形式,绘制单选按钮,网络上也有很多这样的文章,但一般情况下自定义的控件在界面美观性、效果方面稍有欠缺。

因此,我们打算用CheckBox+LinearLayout来实现一种多行单选按钮组的效果。

效果如下:

demo

思路

Android中要实现单选按钮要用到RadioGroup+RadioButton的布局结构。

RadioGroup继承自LinearLayout,只能按单行或单列的形式排布RadioButton组,所以只能通过其他方法来解决。

RadioButton如果不放置在RadioGroup中,则功能类似于CheckBox,只是不能取消选择。

鉴于上述两个原因,单选组的实现中使用CheckBox控件,更利于封装和统一,因为封装的方法复选框也可以用。

单选组

示例中的单选组采用横向分布形式,第一行使用LinearLayout做横向布局,里面放置5个CheckBox;第二行使用LinearLayout做横向布局,里面放置2个CheckBox。

布局结构

  • LinearLayout
    • CheckBox
    • CheckBox
    • CheckBox
    • CheckBox
    • CheckBox
  • LinearLayout
    • CheckBox
    • CheckBox

相关布局xml源码:

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginTop="20dip">

    <CheckBox android:id="@+id/radio1" style="@style/select_style" android:tag="rd1" android:text="单选1" />

    <CheckBox android:id="@+id/radio2" style="@style/select_style" android:tag="rd2" android:text="单选2" />

    <CheckBox android:id="@+id/radio3" style="@style/select_style" android:tag="rd3" android:text="单选3" />

    <CheckBox android:id="@+id/radio4" style="@style/select_style" android:tag="rd4" android:text="单选4" />

    <CheckBox android:id="@+id/radio5" style="@style/select_style" android:tag="rd5" android:text="单选5" />

</LinearLayout>

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginTop="10dip">

    <CheckBox android:id="@+id/radio6" style="@style/select_style" android:tag="rd6" android:text="单选6" />

    <CheckBox android:id="@+id/radio7" style="@style/select_style" android:tag="rd7" android:text="单选7" />

</LinearLayout>

单选控制

因为CheckBox本身是复选框,如果把一组CheckBox做成单选模式的话需要监听CheckBox的Click事件,这个地方用Click事件更合适一点,因为监听的是“被点击”而不是“值改变”。
单选控制要在Activity中注册事件:

/** * 单选项点击事件 * @param checkBox */
@OnClick({ 
   R.id.radio1, R.id.radio2, R.id.radio3, R.id.radio4, R.id.radio5, R.id.radio6, R.id.radio7})
void changeRadios(CheckBox checkBox) { 
   
    CommonUtil.unCheck(radios);
    checkBox.setChecked(true);
}

说明:

CommonUtil.unCheck(radios); 取消所有单选组CheckBox的选中状态;

checkBox.setChecked(true); 设置当前CheckBox选中,因为Radio是不能点击自己取消自己选中状态的,这样比较符合设计逻辑。

用到了ButterKnife框架赖绑定click事件,对一组View来说,绑定起来比较方便。

其中:

CommonUtil.unCheck方法:

/** * 取消checkbox选中状态 * * @param checkBoxList 复选框列表 */
public static void unCheck(List<CheckBox> checkBoxList) { 
   
    for (CheckBox chb : checkBoxList) { 
   
        chb.setChecked(false);
    }
}

获取选中值

因为单选效果是我们通过CheckBox虚拟出来的,所以按钮的选中值也得做出相应修改,这里定义了一个工具方法:

/** * 获取单选值 * * @param checkBoxList * @return String 单选值 */
public static String getOne(List<CheckBox> checkBoxList) { 
   
    String tag = "";
    for (CheckBox chb : checkBoxList) { 
   
        if (chb.getTag() == null) { 
   
            continue;
        }
        if (chb.isChecked()) { 
   
            tag = chb.getTag().toString();
            break;
        }
    }
    return tag;
}

说明:

CheckBox的值是通过tag属性来取得,在界面布局时给CheckBox已经加上了tag。

复选组

Android提供的CheckBox就可以提供复选功能,只是多数情况下我们需要使用一组CheckBox,并获取选中的所有值。

布局结构

借鉴单选组的构建方法,复选组的界面xml:

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginTop="20dip">

    <CheckBox android:id="@+id/checkbox1" style="@style/select_style" android:tag="chb1" android:text="复选1" />

    <CheckBox android:id="@+id/checkbox2" style="@style/select_style" android:tag="chb2" android:text="复选2" />

    <CheckBox android:id="@+id/checkbox3" style="@style/select_style" android:tag="chb3" android:text="复选3" />

    <CheckBox android:id="@+id/checkbox4" style="@style/select_style" android:tag="chb4" android:text="复选4" />

    <CheckBox android:id="@+id/checkbox5" style="@style/select_style" android:tag="chb5" android:text="复选5" />

</LinearLayout>

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginTop="10dip">

    <CheckBox android:id="@+id/checkbox6" style="@style/select_style" android:tag="chb6" android:text="复选6" />

    <CheckBox android:id="@+id/checkbox7" style="@style/select_style" android:tag="chb7" android:text="复选7" />

</LinearLayout>

获取选中值

提前将一系列CheckBox定义到一个集合中,然后通过遍历集合中已选中的CheckBox,就可以获得选中的值了,这里同样将value存放在tag中。

/** * 获取多选值 * * @param checkBoxList * @return String 多个值结合,逗号分隔 */
public static String getMany(List<CheckBox> checkBoxList) { 
   
    StringBuffer sb = new StringBuffer();
    for (CheckBox chb : checkBoxList) { 
   
        if (chb.getTag() == null) { 
   
            continue;
        }
        if (chb.isChecked()) { 
   
            if (sb.length() > 0) { 
   
                sb.append(", ");
            }
            sb.append(chb.getTag().toString());
        }
    }
    return sb.toString();
}

样式美化

示例中去除了图标,定义了CheckBox的样式select_style:

<!-- 选择框自定义主题 -->
<style name="select_style"> <item name="android:layout_width">60dip</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_marginLeft">4dip</item> <item name="android:layout_marginRight">4dip</item> <item name="android:paddingTop">4dip</item> <item name="android:paddingBottom">4dip</item> <item name="android:background">@drawable/select_selector</item> <item name="android:button">@null</item> <item name="android:gravity">center</item> <item name="android:textSize">13sp</item> <item name="android:textColor">@drawable/txt_select_selector</item> </style>

实际使用过程可通过android:drawableLeft等属性为单选组、复选组添加小图标,满足项目需求。

Activity源码

package com.dommy.selectcustom;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.dommy.selectcustom.util.CommonUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity { 
   
    @BindView(R.id.txt_value)
    TextView tvValue; // 选种值
    @BindViews({ 
   R.id.radio1, R.id.radio2, R.id.radio3, R.id.radio4, R.id.radio5, R.id.radio6, R.id.radio7})
    List<CheckBox> radios; // 单选组
    @BindViews({ 
   R.id.checkbox1, R.id.checkbox2, R.id.checkbox3, R.id.checkbox4, R.id.checkbox5, R.id.checkbox6, R.id.checkbox7})
    List<CheckBox> checkBoxes; // 多选组

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        // 如果有初始状态需要显示,参见:
        // com.dommy.selectcustom.util.CommonUtil.checkOne()
        // com.dommy.selectcustom.util.CommonUtil.checkMany()
    }

    /** * 单选项点击事件 * @param checkBox */
    @OnClick({ 
   R.id.radio1, R.id.radio2, R.id.radio3, R.id.radio4, R.id.radio5, R.id.radio6, R.id.radio7})
    void changeRadios(CheckBox checkBox) { 
   
        CommonUtil.unCheck(radios);
        checkBox.setChecked(true);

        // 显示选中项值
        String checkedValues = CommonUtil.getOne(radios);
        tvValue.setText("选中了:" + checkedValues);
    }

    /** * 复选项点击事件 * @param checkBox */
    @OnClick({ 
   R.id.checkbox1, R.id.checkbox2, R.id.checkbox3, R.id.checkbox4, R.id.checkbox5, R.id.checkbox6, R.id.checkbox7})
    void changeCheckBoxs(CheckBox checkBox) { 
   
        // 显示选中项值
        String checkedValues = CommonUtil.getMany(checkBoxes);
        tvValue.setText("选中了:" + checkedValues);
    }
}

项目源码

https://github.com/ahuyangdong/SelectCustom

CSDN下载:
https://download.csdn.net/download/ahuyangdong/10722882

最新更新

想支持Checkbox的自适应布局呈现吗?见最新文章:Android实现自由单选、复选按钮效果+样式美化(二)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146202.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python字符串匹配开头_对python 匹配字符串开头和结尾的方法详解

    python字符串匹配开头_对python 匹配字符串开头和结尾的方法详解1、你需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URLScheme等等。检查字符串开头或结尾的一个简单方法是使用str.startswith()或者是str.endswith()方法。比如:>>>filename=’spam.txt’>>>filename.endswith(‘.tx…

    2022年7月15日
    16
  • 环境贴图_HDR高清环境贴图

    环境贴图_HDR高清环境贴图以前自己看过shader,最近因为被客户逼着搞效果,只能自个儿捣鼓shader。好友把我深深鄙视一番。只好自己单独写篇环境贴图的文章,来小总结一下。环境贴图(EnvironmentMapping)

    2022年8月1日
    1
  • 【mysql系列】细谈explain执行计划之“谜”

    【mysql系列】细谈explain执行计划之“谜”目录理论Part概念实践Partidselect_typetabletypepossible_keyskeykey_lenrefrowsfilteredExtra总结聊到mysql数据库的优化,大家基本都会谈论explain关键字,确认sql是否使用数据库表中建立的索引,然后讨论sql语句或者索引优化方案等等~,那本篇文章主要谈论一下explain的理解。主要分为理论和实践相结合。理论Part概念我们先了解一下explain语法和相关理论知识。语法:EXPLAINSELECTselect_o

    2025年6月16日
    0
  • pandas中的drop函数_pandas replace函数

    pandas中的drop函数_pandas replace函数这里写自定义目录标题新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML图表FLowchart流程图导出与导入导出导入新的改变我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

    2022年9月16日
    0
  • Android自己主动化測试解决方式

    Android自己主动化測试解决方式

    2021年11月15日
    40
  • JVM类加载机制、双亲委派机制、自定义类加载器、打破双亲委派机制[通俗易懂]

    JVM类加载机制、双亲委派机制、自定义类加载器、打破双亲委派机制[通俗易懂]1、类加载器站在Java虚拟机的角度看,只有两种不同的类加载器:一种是启动类加载器(BootstrapClassLoader),这个类加载器使用C++语言实现(HotSpot虚拟机、JDK8中),是虚拟机自身的一部分;另外一种是其他所有类加载器,这些类加载器都由Java语言实现,独立存在于虚拟机外部,并且全部继承自抽象类java.lang.ClassLoaderJDK8及以前版本中绝大多数程序都会使用到以下3个系统提供的类加载器来进行加载启动类(引导类)加载器:负责加载支撑JVM运行的位于&l

    2022年9月4日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号