怎么推广网站链接,sem网站建设,上海自助建网站,深圳知名包装设计公司CheckBox选择Or不选#xff0c;是个问题#xff01;
前言
前面我们讲过了RadioButton与RadioGroup#xff0c;利用单选按钮组的属性来实现仿微信底部Tab切换的效果。对比记忆一下#xff0c;今天我们来讲解第二个类似的控件CheckBox#xff0c;按照惯例先看下它的类继承…CheckBox选择Or不选是个问题
前言
前面我们讲过了RadioButton与RadioGroup利用单选按钮组的属性来实现仿微信底部Tab切换的效果。对比记忆一下今天我们来讲解第二个类似的控件CheckBox按照惯例先看下它的类继承关系如下
public class CheckBox extends CompoundButton
java.lang.Object↳ android.view.View↳ android.widget.TextView↳ android.widget.Button↳ android.widget.CompoundButton↳ android.widget.CheckBox我们发现CheckBox与RadioButton有相同的继承关系所以CheckBox也是一个具有选中效果的控件通常我们称它为**复选框**。
基本使用
先来展示一段代码展示下效果。
?xml version1.0 encodingutf-8?
android.support.constraint.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentCheckBoxapp:layout_constraintHorizontal_chainStylepackedandroid:idid/cb_hobbyandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:checkedtrueapp:layout_constraintRight_toLeftOfid/tv_hobbyapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintLeft_toLeftOfparentapp:layout_constraintTop_toTopOfparent /TextViewandroid:idid/tv_hobbyandroid:layout_widthwrap_contentandroid:layout_marginLeft5dpandroid:layout_heightwrap_contentapp:layout_constraintTop_toTopOfparentapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintLeft_toRightOfid/cb_hobbyandroid:text游泳app:layout_constraintRight_toRightOfparent /
/android.support.constraint.ConstraintLayout这里我们使用了前面博文内容讲到的ConstraintLayout实现了CheckBox和TextView一起居中整个父布局的效果。如果你还不是很熟悉这个约束布局如何使用可以查看之前博文内容《布局大杀器—ConstraintLayout》 实现效果如图所示 这里默认设置CheckBox的checked属性为true则表示默认选中那么在页面中如何获取这个控件是否被选中呢当然是通过设置监听器这里附上代码
/*** 演示CheckBox等用法** author xmkh*/
public class CheckActivity extends AppCompatActivity {Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_check);CheckBox cbHobby findViewById(R.id.cb_hobby);final TextView tvHobby findViewById(R.id.tv_hobby);//设置复选框的勾选状态监听器cbHobby.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {tvHobby.setText(isChecked ? 已选中 : 未选中);}});}
}
实现效果如图所示 实践
实际效果中我们一般不会使用自带的样式同样的我们参照RadioButton的方式来给它设置一个UI样式。通常在注册界面总会看到是否同意《用户注册协议》的复选框如果要实现下图的样式我们怎么做呢 我们来仿照这个效果实现一下界面布局。
我们准备选中和未选中2个图片ic_login_agreement_check.png和ic_login_agreement_uncheck.png
在res/drawable/文件夹下新建一个样式文件selector_cb_login_agreement.xml 附上样式文件代码
?xml version1.0 encodingutf-8?
selector xmlns:androidhttp://schemas.android.com/apk/res/androiditem android:drawablemipmap/ic_login_agreement_check android:state_checkedtrue/item android:drawablemipmap/ic_login_agreement_uncheck /
/selector设置CheckBox的Button样式完整代码如下
?xml version1.0 encodingutf-8?
android.support.constraint.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.RegisterCheckActivity!--主要设置CheckBox的button样式为自定义的selector_cb_login_agreement即可--CheckBoxandroid:idid/cb_login_agreementapp:layout_constraintTop_toTopOfparentapp:layout_constraintBottom_toBottomOfparentandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:checkedtrueandroid:buttondrawable/selector_cb_login_agreementapp:layout_constraintEnd_toStartOfid/tv_login_agreementapp:layout_constraintHorizontal_chainStylepackedapp:layout_constraintStart_toStartOfparent /TextViewandroid:textColor#A6600Candroid:idid/tv_login_agreementandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text我已阅读并同意《XX用户注册协议》android:textSize18spapp:layout_constraintBottom_toBottomOfid/cb_login_agreementapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toEndOfid/cb_login_agreementapp:layout_constraintTop_toTopOfid/cb_login_agreement /
/android.support.constraint.ConstraintLayout最终实现效果如图所示 结语
今天我们的CheckBox分享就到此结束啦希望各位小伙伴在学习Android基础控件的时候能够举一反三多思考、多练习。坚持下去相信你一定会从小白变成大牛的也欢迎各位小伙伴加入我们的微信技术交流群在公众号中回复微信群就可以加入其中也可以在公众号中回复视频里面有一些初学者视频哦~
PS:如果还有未看懂的小伙伴欢迎加入我们的QQ技术交流群892271582里面有各种大神回答小伙伴们遇到的问题我们的微信群马上也将要和大家见面啦届时希望大家踊跃加入其中~~