700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > CheckBox实现单选 多选等多项功能

CheckBox实现单选 多选等多项功能

时间:2022-07-31 18:42:26

相关推荐

CheckBox实现单选 多选等多项功能

1、CheckBox复选框的基本使用:

一般简单的使用,当需要选择多项时,可以考虑将其使用,比如兴趣的选择,一个人有多个选择结果,这时候我们设置复选框进行选择。

布局与简单的使用:设置监听,set

<CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textColor="@color/black"android:text="地层界线"android:id="@+id/gb_0"/>gb_0.setOnCheckedChangeListener(myCheckedChangedListener); //设置监听

2、包含单选、多选

项目使用中,点击一个控件,弹出一个选择列表,有时只需要选择一个,有时候需要选择两个,且最多只包含两个。作者使用checkBox实现了基本的功能,为了避免以后使用,于是做个记录。

结合Alertdialog通过自定义的View弹出选择结果,下面将分别做一个记录:

1、自定义布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textColor="@color/black"android:text="地层界线"android:id="@+id/gb_0"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textColor="@color/black"android:layout_below="@id/gb_0"android:text="岩性界线"android:id="@+id/gb_1"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textColor="@color/black"android:layout_below="@id/gb_1"android:text="断裂出露线"android:id="@+id/gb_2"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textColor="@color/black"android:layout_below="@id/gb_2"android:text="褶皱轴线"android:id="@+id/gb_3"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textColor="@color/black"android:layout_below="@id/gb_3"android:text="阶地界线"android:id="@+id/gb_4"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textColor="@color/black"android:layout_below="@id/gb_4"android:text="地质现象范围线"android:id="@+id/gb_5"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textColor="@color/black"android:layout_below="@id/gb_5"android:text="料场范围线"android:id="@+id/gb_6"/></RelativeLayout>

2、与AlertDialog结合

AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("选择地质界线:");View view1 = LayoutInflater.from(context).inflate(R.layout.gb_line_chose_layout,null);builder.setView(view1); //传入布局//checkBox控件初始化以及注册监听CheckBox gb_0 = view1.findViewById(R.id.gb_0);checkBoxList.add(gb_0);CheckBox gb_1 = view1.findViewById(R.id.gb_1);checkBoxList.add(gb_1);CheckBox gb_2 = view1.findViewById(R.id.gb_2);checkBoxList.add(gb_2);CheckBox gb_3 = view1.findViewById(R.id.gb_3);checkBoxList.add(gb_3);CheckBox gb_4 = view1.findViewById(R.id.gb_4);checkBoxList.add(gb_4);CheckBox gb_5 = view1.findViewById(R.id.gb_5);checkBoxList.add(gb_5);CheckBox gb_6 = view1.findViewById(R.id.gb_6);checkBoxList.add(gb_6);MyCheckedChangedListener myCheckedChangedListener = new MyCheckedChangedListener(checkBoxList);gb_0.setOnCheckedChangeListener(myCheckedChangedListener);gb_1.setOnCheckedChangeListener(myCheckedChangedListener);gb_2.setOnCheckedChangeListener(myCheckedChangedListener);gb_3.setOnCheckedChangeListener(myCheckedChangedListener);gb_4.setOnCheckedChangeListener(myCheckedChangedListener);gb_5.setOnCheckedChangeListener(myCheckedChangedListener);gb_6.setOnCheckedChangeListener(myCheckedChangedListener);builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {index = new ArrayList<>(); //存放index 0-6,分别代表,后期编录也需要for(int i =0;i< checkBoxList.size();i++){if(checkBoxList.get(i).isChecked())index.add(i);}if(index.size()==0){dismiss();}else {String [] titile_array = {"地层界线","岩性界线","断裂出露线","褶皱轴线","阶地界线","地质现象范围线","料场范围线"};List<String> name_list = new ArrayList<>(); //此时选中的地质界线的名字for(int j =0 ;j< index.size();j++){name_list.add(titile_array[index.get(j)]);}drawCatalog = 2;if(!name_list.contains(dataset.getName())){Toast.makeText(context,"选中的图层都不可编辑",Toast.LENGTH_SHORT).show();return;}mapControl.setAction(Action.CREATEPOLYLINE);mapControl.setGestureDetector(new GestureDetector(context,new MySimpleOnGestureListener()));Toast.makeText(context,"点击屏幕选点编录",Toast.LENGTH_SHORT).show();dismiss();}}});AlertDialog dialog = builder.create();dialog.show();dialog.setCanceledOnTouchOutside(false); //点击外部不可以进行取消Window window = dialog.getWindow();window.setBackgroundDrawableResource(R.drawable.save_parameter_btn_baackground);window.setLayout(context.getResources().getDisplayMetrics().widthPixels*4/5, LinearLayout.LayoutParams.WRAP_CONTENT);

3、自定义的监听类MyCheckedChangedListener

private class MyCheckedChangedListener implements CompoundButton.OnCheckedChangeListener{private List<CheckBox> checkBoxList; //构造函数,按顺序排好的全部的CHeckBox组private int time =0; //用于控制选中的个数,后面以其做判断public MyCheckedChangedListener(List<CheckBox>list){checkBoxList = list;}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {//每次选中一个,我们的time就+1,最大为2if(isChecked){time+=1;if(time<2){switch (buttonView.getId()){case R.id.gb_0://根据关联规则,根据不同的index,设置某些checjkbox不可选择operateCheckBox(0,checkBoxList); break;case R.id.gb_1:operateCheckBox(1,checkBoxList);break;case R.id.gb_2:operateCheckBox(2,checkBoxList);break;case R.id.gb_3:operateCheckBox(3,checkBoxList);break;case R.id.gb_4:operateCheckBox(4,checkBoxList);break;case R.id.gb_5:operateCheckBox(5,checkBoxList);break;case R.id.gb_6:operateCheckBox(6,checkBoxList);break;}} //如果达到最大的2,设置其他全部的都不可选择,同时更新颜色,直观展示else if(time == 2){for(CheckBox checkBox : checkBoxList){if(!checkBox.isChecked()){checkBox.setClickable(false);}}//设置TextCOlor使其一眼就能看出是否选择:不可选设置为灰色;可以选择则为红色;updateState(checkBoxList);}}//此时表示某个CheckBox取消了选择,则time需要减小1else {time-=1;//当为1的时候,则我们按照上面为1一样的逻辑进行处理if(time==1){for(CheckBox checkBox : checkBoxList){if(checkBox.isChecked()){//此处较为关键,用于更新int inde = checkBoxList.indexOf(checkBox); operateCheckBox(inde,checkBoxList);}}} //time为0,我们则设置全可以选择else {for(CheckBox checkBox : checkBoxList){checkBox.setClickable(true);}updateState(checkBoxList);}}}//根据关联规则,更新可选/不可选private void operateCheckBox(int index, List<CheckBox> checkBoxList) {for(CheckBox checkBox : checkBoxList){checkBox.setClickable(true);}switch (index){case 0:int [] temp0 = {1,3,6};for(int i : temp0){checkBoxList.get(i).setClickable(false);}break;case 1:int [] temp1 = {0,3,6};for(int i : temp1){checkBoxList.get(i).setClickable(false);}break;case 2:int [] temp2 = {3,4,5,6};for(int i : temp2){checkBoxList.get(i).setClickable(false);checkBoxList.get(i).setTextColor(Color.parseColor("#789456"));}break;case 3:int [] temp3 = {0,1,2,4,5,6};for(int i : temp3){checkBoxList.get(i).setClickable(false);}break;case 4:int [] temp4 = {2,3,5,6};for(int i : temp4){checkBoxList.get(i).setClickable(false);}break;case 5:int [] temp5 = {2,3,4,6};for(int i : temp5){checkBoxList.get(i).setClickable(false);}break;case 6:int [] temp6 = {0,1,2,3,4,5};for(int i : temp6){checkBoxList.get(i).setClickable(false);}break;}updateState(checkBoxList);}//根据是否可以点击,咱们设置颜色,更加直观。private void updateState(List<CheckBox> checkBoxList1){for(CheckBox checkBox : checkBoxList1){if(!checkBox.isClickable()){checkBox.setTextColor(Color.parseColor("#CACAC9"));}else {checkBox.setTextColor(Color.parseColor("#000000"));}}}}

4、效果展示:

由于无法上传视频,则无法展示,个人感觉效果还是不错的,既满足单选也能实现需要的两项选择。只能通过gif动图的形式展示出来,希望有所帮助。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。