当前位置: 首页 > news >正文

Android RadioButton和CheckBox控件

1. RadioButton类

RadioButton继承CompoundButton,是单选按钮。
android:checked设置按钮是否选中。用setOnCheckedChangeListener(OnCheckedChangeListener)来对单选按钮进行监听。
这里写图片描述
RadioButton选择以后不能取消,主要是它覆盖了toggle方法,使其在被选中后无法取消。但可通过setChecked(boolean)方法取消。

public class RadioButton extends CompoundButton {@Overridepublic void toggle() {// we override to prevent toggle when the radio is already// checked (as opposed to check boxes widgets)if (!isChecked()) {super.toggle();}}
}

RadioButton可以和RadioGroup配合使用,RadioButton只可以选择一个从而达到了单选的目的。android:checkedButton指定初始选项。

<RadioGroupandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:checkedButton="@+id/rb1"><RadioButtonandroid:id="@+id/rb1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="足球"/><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="篮球"/>
</RadioGroup>

效果如下
这里写图片描述
RadioGroup的构造方法中,调用了init()方法,指定CheckedStateTrackerPassThroughHierarchyChangeListener

private void init() {mChildOnCheckedChangeListener = new CheckedStateTracker();mPassThroughListener = new PassThroughHierarchyChangeListener();super.setOnHierarchyChangeListener(mPassThroughListener);
}

PassThroughHierarchyChangeListener类监听RadioGroupView的状态,对每个RadioButton添加CheckedStateTracker监听器。

private class PassThroughHierarchyChangeListener implementsViewGroup.OnHierarchyChangeListener {@Overridepublic void onChildViewAdded(View parent, View child) {if (parent == RadioGroup.this && child instanceof RadioButton) {int id = child.getId();// generates an id if it's missingif (id == View.NO_ID) {id = View.generateViewId();child.setId(id);}((RadioButton) child).setOnCheckedChangeWidgetListener(mChildOnCheckedChangeListener);}if (mOnHierarchyChangeListener != null) {mOnHierarchyChangeListener.onChildViewAdded(parent, child);}}
}

CheckedStateTracker类,取消原来的选项并设置新的选项。

private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// prevents from infinite recursionif (mProtectFromCheckedChange) {return;}mProtectFromCheckedChange = true;if (mCheckedId != -1) {setCheckedStateForView(mCheckedId, false);}mProtectFromCheckedChange = false;int id = buttonView.getId();setCheckedId(id);}
}// 取消原来的选项
private void setCheckedStateForView(int viewId, boolean checked) {View checkedView = findViewById(viewId);if (checkedView != null && checkedView instanceof RadioButton) {((RadioButton) checkedView).setChecked(checked);}
}// 设置新的选项
private void setCheckedId(@IdRes int id) {mCheckedId = id;if (mOnCheckedChangeListener != null) {mOnCheckedChangeListener.onCheckedChanged(this, mCheckedId);}final AutofillManager afm = mContext.getSystemService(AutofillManager.class);if (afm != null) {afm.notifyValueChanged(this);}
}

2. CheckBox类

CheckBox继承CompoundButton,是多选按钮。
android:checked设置按钮是否选中。用setOnCheckedChangeListener(OnCheckedChangeListener)来对多选按钮进行监听。
这里写图片描述

3. 自定义按钮

RadioButtonCheckBox可以通过android:button指定按钮样式。样式文件check_box_default.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true"android:drawable="@drawable/checkbox_s" /><item android:state_pressed="true"android:drawable="@drawable/checkbox_s" /><itemandroid:drawable="@drawable/checkbox" />
</selector>

布局文件

<CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="足球"android:padding="6dp"android:button="@drawable/check_box_default"/>
<CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="篮球"android:padding="6dp"android:button="@drawable/check_box_default" />

效果如下
这里写图片描述

相关文章
Android RadioButton和CheckBox控件
Android Switch和ToggleButton控件

http://www.xdnf.cn/news/827353.html

相关文章:

  • Tombstone原理分析
  • 伺服电机驱动原理
  • 移动硬盘怎么加密?移动硬盘加密软件有哪些?
  • next数组、nextval数组的推导及C语言实现
  • 135 、137、139端口等主要用途
  • 安装教程 PyG(PyTorch Geometric)
  • C++------static_cast和dynamic_cast详解
  • HTML网页制作知识总结(简单易学)
  • 【建议收藏】Windows批处理(cmd/bat)常用命令总结
  • ubuntu下安装jdk
  • 关于代码评审(CodeReview)那些不得不说的事儿
  • 10大办公常用工具\网址分享(收藏一波)
  • 都2023了还不知道怎么GCC,今天就来教大家如何安装GCC
  • 关于代码混淆,看这篇就够了
  • 决策树模型
  • JAVA DecimalFormat 保留小数位以及四舍五入的陷阱
  • 了解Beamforming
  • 卡巴斯基 注册码
  • JAVA中的API文档
  • SDK、API、Open API有什么区别(iot开发平台)
  • HBase之Compaction
  • 内网IP如何查看?
  • Fiddler4使用教程
  • 【全网】Nginx最全使用教程
  • linux环境下载文件
  • Oracle如何新建用户
  • 《深入浅出Dart》Flutter中的Material和Cupertino组件
  • java中异常详解以及运行时异常runtime exception
  • mySQL常见命令
  • Nature综述:肠道菌群如何划分肠型