Checkbox案例:多选题
效果图:
未选择状态
点击按钮之后才会出现提交按钮:
答对后出现刷新按钮:
答错后出现正确答案和刷新按钮:
逻辑代码:
在这里部分代码使用了butterknife插件
butterknife配置参考链接:
https://blog.csdn.net/weixin_43671437/article/details/105150307
package com.example.yuanjiao;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
public class yuanjiao extends AppCompatActivity {
@BindView(R.id.checkbox_1)
CheckBox checkbox1;
@BindView(R.id.checkbox_2)
CheckBox checkbox2;
@BindView(R.id.checkbox_3)
CheckBox checkbox3;
@BindView(R.id.submit)
Button submit;
@BindView(R.id.tv_true)
TextView tvTrue;
@BindView(R.id.btn_refresh)
Button btnRefresh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yuanjiao);
ButterKnife.bind(this);
}
@SuppressLint("WrongConstant")
@OnCheckedChanged({R.id.checkbox_1, R.id.checkbox_2, R.id.checkbox_3})
public void checkChange(CompoundButton view, boolean isChecked) {
switch (view.getId()) {
case R.id.checkbox_1:
action();
//2.当选中任意一个后,背景颜色和字体颜色会改变
if (isChecked) {
checkbox1.setBackgroundColor(new Color().parseColor("#F8F5ED"));
checkbox1.setTextColor(new Color().parseColor("#AF9F92"));
break;
}
checkbox1.setBackgroundDrawable(getResources().getDrawable(R.drawable.radius));
checkbox1.setTextColor(new Color().parseColor("#000000"));
break;
case R.id.checkbox_2:
action();
if (isChecked) {
checkbox2.setBackgroundColor(new Color().parseColor("#F8F5ED"));
checkbox2.setTextColor(new Color().parseColor("#AF9F92"));
break;
}
checkbox2.setBackgroundDrawable(getResources().getDrawable(R.drawable.radius));
checkbox2.setTextColor(new Color().parseColor("#000000"));
break;
case R.id.checkbox_3:
action();
if (isChecked) {
checkbox3.setBackgroundColor(new Color().parseColor("#F8F5ED"));
checkbox3.setTextColor(new Color().parseColor("#AF9F92"));
break;
}
checkbox3.setBackgroundDrawable(getResources().getDrawable(R.drawable.radius));
checkbox3.setTextColor(new Color().parseColor("#000000"));
break;
}
}
/**
* 1.当a b c选中任意一个,按钮都会显示出来
* 2.当选中任意一个后,背景颜色和字体颜色会改变
* 3.点击提交后比对答案是否正确:
* 正确:把正确答案标绿;错误:把选项的标红,显示出正确答案
*/
@SuppressLint("WrongConstant")
public void action() {
//1.当a b c选中任意一个按钮都会显示出来
if (checkbox1.isChecked() || checkbox2.isChecked() || checkbox3.isChecked()) {
submit.setVisibility(0);
} else {
submit.setVisibility(4);
}
}
@SuppressLint("WrongConstant")
@OnClick({R.id.submit, R.id.btn_refresh})
public void onViewClicked(View v) {
switch (v.getId()){
case R.id.submit:
btnRefresh.setVisibility(0);
//3.点击提交后比对答案是否正确
if (checkbox1.isChecked() && checkbox2.isChecked() && !checkbox3.isChecked()) {
checkbox1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
checkbox2.setBackgroundColor(getResources().getColor(R.color.colorAccent));
Toast.makeText(this,"Congratulations",Toast.LENGTH_SHORT).show();
} else {
if (checkbox1.isChecked()) checkbox1.setBackgroundColor(new Color().RED);
if (checkbox2.isChecked()) checkbox2.setBackgroundColor(new Color().RED);
if (checkbox3.isChecked()) checkbox3.setBackgroundColor(new Color().RED);
tvTrue.setText("正确答案是:A B");
}
break;
//刷新按钮,点击后恢复原状
case R.id.btn_refresh:
checkbox1.setBackgroundDrawable(getResources().getDrawable(R.drawable.radius));
checkbox1.setTextColor(new Color().parseColor("#000000"));
checkbox2.setBackgroundDrawable(getResources().getDrawable(R.drawable.radius));
checkbox2.setTextColor(new Color().parseColor("#000000"));
checkbox3.setBackgroundDrawable(getResources().getDrawable(R.drawable.radius));
checkbox3.setTextColor(new Color().parseColor("#000000"));
checkbox1.setChecked(false);
checkbox2.setChecked(false);
checkbox3.setChecked(false);
tvTrue.setText("");
btnRefresh.setVisibility(4);
break;
}
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".yuanjiao">
<!--复选按钮
android:checked="false"
android:button="@null" 可取消图标
-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1、android:orientation的属性值有那几个?"
android:textSize="15sp"
android:layout_margin="10dp"
/>
<CheckBox
android:id="@+id/checkbox_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A. vertical"
android:button="@null"
android:layout_margin="10dp"
android:padding="10dp"
android:background="@drawable/radius"
/>
<CheckBox
android:id="@+id/checkbox_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="B. horizontal"
android:button="@null"
android:padding="10dp"
android:layout_margin="10dp"
android:background="@drawable/radius"
/>
<CheckBox
android:id="@+id/checkbox_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="C. match_parent"
android:button="@null"
android:layout_margin="10dp"
android:padding="10dp"
android:background="@drawable/radius"
/>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="submit"
android:textSize="20dp"
android:textAllCaps="false"
android:visibility="invisible"
android:layout_margin="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
/>
<Button
android:id="@+id/btn_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刷新"
android:visibility="invisible"
/>
</LinearLayout>
</LinearLayout>
shape文件:
radius(规范命名shape_radius)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--设置背景颜色-->
<solid android:color="#FBF5F5"/>
<!--设置圆角-->
<corners android:radius="7dp"/>
<!--设置边框颜色和宽度-->
<stroke android:color="#CCCC" android:width="2dp" />
</shape>