首页 > 技术文章 > 第十六篇-使用CheckBox实现多项选择

smart-zihan 2018-10-25 14:01 原文

话不多说,先上效果图

屏幕截图方法,全屏截图按键盘print screen就行,活动窗口截图,按住ALT+print screen。

图片默认保存在home/picture路径下。可以通过自带的图片处理软件shotwell打开

先建一个empty Activity的项目,然后修改MainActivity.java,res/String.xml,activity_main.xml。

MainActivity.java

package com.example.aimee.checkboxtest;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    CheckBox checkbox1;
    CheckBox checkbox2;
    CheckBox checkbox3;
    CheckBox checkbox4;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkbox1=(CheckBox) findViewById(R.id.checkBox01);
        checkbox2=(CheckBox) findViewById(R.id.checkBox02);
        checkbox3=(CheckBox) findViewById(R.id.checkBox03);
        checkbox4=(CheckBox) findViewById(R.id.checkBox04);
        button=(Button) findViewById(R.id.Submit);
        checkbox1.setOnCheckedChangeListener(new CheckBoxListener());
        checkbox2.setOnCheckedChangeListener(new CheckBoxListener());
        checkbox3.setOnCheckedChangeListener(new CheckBoxListener());
        checkbox4.setOnCheckedChangeListener(new CheckBoxListener());
        button.setOnClickListener((View.OnClickListener) new ButtonClickListener());
    }

    class CheckBoxListener implements CompoundButton.OnCheckedChangeListener {
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
            if(isChecked){
                Toast toast=Toast.makeText(MainActivity.this,buttonView.getText()+"被选择",Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER,5,5);
                toast.show();
            }else{
                Toast toast=Toast.makeText(MainActivity.this,buttonView.getText()+"取消选择",Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER,5,5);
                toast.show();
            }
        }
    }
    class ButtonClickListener implements View.OnClickListener{
        public void onClick(View arg0){
            String str="";
            if(checkbox1.isChecked())
                str=str+checkbox1.getText();
            if(checkbox2.isChecked())
                str=str+checkbox2.getText();
            if(checkbox3.isChecked())
                str=str+checkbox3.getText();
            if(checkbox4.isChecked())
                str=str+checkbox4.getText();
            Toast.makeText(MainActivity.this,str+"被选择",Toast.LENGTH_LONG).show();
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Title" />

    <CheckBox
        android:id="@+id/checkBox01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="@string/Profile1"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="135dp" />

    <CheckBox
        android:id="@+id/checkBox02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="@string/Profile2"
        app:layout_constraintTop_toBottomOf="@+id/checkBox01"
        tools:layout_editor_absoluteX="135dp"
        tools:ignore="MissingConstraints" />

    <CheckBox
        android:id="@+id/checkBox03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="@string/Profile3"
        app:layout_constraintTop_toBottomOf="@+id/checkBox02"
        tools:layout_editor_absoluteX="135dp"
        tools:ignore="MissingConstraints" />

    <CheckBox
        android:id="@+id/checkBox04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="@string/Profile4"
        app:layout_constraintTop_toBottomOf="@+id/checkBox03"
        tools:layout_editor_absoluteX="135dp"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/Submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:text="@string/Submit"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

 String.xml

<resources>
    <string name="app_name">CheckboxTest</string>
    <string name="Title">请选择喜欢的情景模式</string>
    <string name="Profile1">上班模式</string>
    <string name="Profile2">家庭模式</string>
    <string name="Profile3">旅游模式</string>
    <string name="Profile4">会议模式</string>
    <string name="Submit">Submit</string>
</resources>

 至此,一个简单的多项选择就完成了。

有兴趣也可以设置其他的项目。

 

推荐阅读