【Android】CheckBox实现和监听
三三想成为安卓糕手
一:自定义勾选控件
重在思路:怎么去用已经学习过的知识解决现有的问题。这才是学到了编程的精髓所在
怎么去实现用户是否勾选了协议呢?这里有一个人机交互的动作。
1:创建一个新的类作为控件类
创建新项目
shift+f6修改名称,其它所有有引用的地方都修改过来了
我们自己创建一个类去实现组合控件drawable和text文本的组合(悟了悟了,所以之前DrawableLeft是在这里使用到了,妙啊妙啊!)
public class MyCheckView extends TextView {}public class MyCheckView extends androidx.appcompat.widget.AppCompatTextView {}
TextView ≈ androidx.appcompat.widget.AppCompatTextView
//后者会有一些更加高级的功能(前方的路以后再来探索吧)
2:自定义类包名要完整
xmlns:android="http://schemas.android.com/apk/res/android"
//这是Android系统命名空间声明
//意思:“接下来用到 android: 开头的属性(比如 android:id、android:layout_width )
//都到这个官方命名空间里找定义” 。
有了这行声明,XML 里用系统内置控件(如 TextView
、Button
)时,默认就能识别 android.widget
这个包路径 ,所以可以直接写 <TextView .../>
,不用写全 android.widget.TextView
。
反之自定义控件,因为系统不能识别出来,所以我们在写路径时:完整包名 + 类名
<TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"/><com.xlong.myapplication.MyCheckViewandroid:layout_width="match_parent"android:layout_height="wrap_content" />
实现效果如下
3:代码总结
这就是我们自定义了一个可勾选check类
(1)MyCheckView
public class MyCheckView extends androidx.appcompat.widget.AppCompatTextView {private final Drawable drawableSelect;private final Drawable drawableUnSelect;private boolean check;public MyCheckView(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);
// setText();
// setTextColor();
// setTextSize();
// setCompoundDrawablesRelativeWithIntrinsicBounds();drawableSelect = ContextCompat.getDrawable(context, R.mipmap.icon_select);drawableUnSelect = ContextCompat.getDrawable(context,R.mipmap.icon_unselect);setCheck(false);}public boolean isCheck(){return check;}public void setCheck(boolean check){this.check = check;if(check){setCompoundDrawablesRelativeWithIntrinsicBounds(drawableSelect,null,null,null);}else{setCompoundDrawablesRelativeWithIntrinsicBounds(drawableUnSelect,null,null,null);}}
}
(2)CheckBoxActivity类进行使用
MyCheckView checkView = findViewById(R.id.check_view);checkView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {boolean isCheck = checkView.isCheck();checkView.setCheck(!isCheck);}});
(3)对应的activity界面xml设置
<com.xlong.myapplication.MyCheckViewandroid:id="@+id/check_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="请勾选我" />
二:Android自带勾选控件
在上一个大标题下,我们使用自定义控件的方式,完成了协议勾选功能;
在安卓当中,已经为我们提供了相关成熟的控件和封装好的方法,兄弟们冲!!!干它丫的
1:源码分析与对比
<CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"/>
继承关系如下
我们重点分析CompoundButton这个类,里面也有一个类似check(布尔类型)勾选的成员变量;
这里我们与自定义的check控件进行一个对比
勾选了怎么做,没有勾选怎么做
注:作为开发者优选选用系统提供给我们的组件,实在没有在选择自己去创建组件
2:Xml定义勾选框
(1)定义CheckBox
<CheckBoxandroid:id="@+id/cb_agreement"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="false"android:text="勾选,表示同意《用户协议》"android:textColor="@color/my_blue"android:textSize="16sp" />
CheckBox
属于一种 UI 组件,呈现为一个小方框,用户点击之后,方框内会出现对勾(✔)
(2)基本属性分析
android:id
:此属性用于给 CheckBox 设定唯一标识符。android:text
:用于设置 CheckBox 旁边显示的文本内容。android:checked
:可将 CheckBox 初始状态设为选中(true
)或者未选中(false
);一般设置为falseandroid:onClick
:能绑定布局文件或者 Activity 里的点击事件处理方法。(这里没有体现,前方的路以后再来探索吧)
(3)定义Button
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btn_login"android:text="登录"/>
3:Java控制CheckBox
(1)代码分析
这里可以与自定义的CheckBox(Java操作)进行对比
((20250704095817-7tynnd6 ‘MyCheckView checkView = findViewById(R.id.check_view); checkView.setOnClickListener(new V…’))
CheckBox cbArgreement = findViewById(R.id.cb_agreement);findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {boolean checked = cbArgreement.isChecked();if(checked){//这句代码是在是太ex了兄弟,我完全是蒙蒙的状态啊xd,窝里哇瓦力哇一袋米扛几楼//判断勾选了协议,点击登录按钮,进行页面跳转startActivity(new Intent(CheckBoxActivity.this,SecondActivity.class));}else{//准备弹窗提示Toast.makeText(CheckBoxActivity.this, "请勾选协议!", Toast.LENGTH_SHORT).show();}}});
效果展示
(2)Toast弹窗提示
Toast.makeText(CheckBoxActivity.this, "请勾选协议!", Toast.LENGTH_SHORT).show();
三个参数:上下文(一般是当前类),弹窗内容,弹窗时间(有长有短)
show方法展示
三:对勾选框状态实时监听
1:类比按钮的监听器
如果需要显示,你现在已经勾选,或者你现在没有勾选,就需要我们实时的对框的状态进行监听
类比思想:以Listener结尾的接口,作用一般都是做某个动作的监听
//按钮Button loginButton = findViewById(R.id.btn_login);loginButton.setOnClickListener(new View.OnClickListener() {setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//这里按钮的监听器的相关代码}});
//CheckBox复选框CheckBox cbArgreement = findViewById(R.id.cb_agreement);cbArgreement.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {}});
2:三种监听方式
//对勾选框进行操作CheckBox cbArgreement = findViewById(R.id.cb_agreement);/*** 实时监听勾选状态*/cbArgreement.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {//方式一boolean checked = cbArgreement.isChecked(); //方式二CheckBox buttonView1 = (CheckBox) buttonView;boolean checked = buttonView1.isChecked();//方式三if(isChecked){Toast.makeText(CheckBoxActivity.this,"感谢您勾选协议",Toast.LENGTH_SHORT).show();}else{Toast.makeText(CheckBoxActivity.this,"请勾选协议",Toast.LENGTH_SHORT).show();}}});
第一种:直接用方法外部定义的CheckBox获取状态,并进行判断
第二种:使用形参buttonView类型为CompoundButton
CheckBox继承CompoundButton继承Button,所以使用向下转型,在调用isChecked()方法获取,check状态
第三种:使用形参isChecked直接进行判断即可