CheckBox案例实现

来源:互联网 发布:网络运维管理系统 编辑:程序博客网 时间:2024/05/17 22:48

这里写图片描述


XML<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="爱好:" />        <CheckBox            android:id="@+id/cb_game"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="电竞" />        <CheckBox            android:id="@+id/cb_travel"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="旅游" />        <CheckBox            android:id="@+id/cb_read"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="阅读" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/btn_all"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="全选" />        <Button            android:id="@+id/btn_notall"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="全不选" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/btn_getResult"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="获取内容" />        <TextView            android:id="@+id/btn_showResult"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="此文本框用于显示结果" />    </LinearLayout></LinearLayout>
Javaimport java.util.ArrayList;...public class MainActivity extends Activity {    private CheckBox cb_game, cb_travel, cb_read;    private CheckBoxListener checkBoxListener;    private Button btn_all, btn_notall, btn_getResult;    private TextView showResult;    private List<String> lists;    private ButtonListener btnButtonListener;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_checkbox);        initView();        initData();        setListener();    }    private void initData() {        // 初始化数据        lists = new ArrayList<String>();    }    private void setListener() {        checkBoxListener = new CheckBoxListener();        cb_game.setOnCheckedChangeListener(checkBoxListener);        cb_travel.setOnCheckedChangeListener(checkBoxListener);        cb_read.setOnCheckedChangeListener(checkBoxListener);        btnButtonListener = new ButtonListener();        btn_all.setOnClickListener(btnButtonListener);        btn_notall.setOnClickListener(btnButtonListener);        btn_getResult.setOnClickListener(btnButtonListener);    }    private void initView() {        // 初始化控件        cb_game = (CheckBox) findViewById(R.id.cb_game);        cb_travel = (CheckBox) findViewById(R.id.cb_travel);        cb_read = (CheckBox) findViewById(R.id.cb_read);        btn_all = (Button) findViewById(R.id.btn_all);        btn_notall = (Button) findViewById(R.id.btn_notall);        btn_getResult = (Button) findViewById(R.id.btn_getResult);        showResult = (TextView) findViewById(R.id.btn_showResult);    }    class CheckBoxListener implements OnCheckedChangeListener {        @Override        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {            // 当选中状态发生变化时出发            CheckBox checkBox = (CheckBox) arg0;            switch (checkBox.getId()) {            case R.id.cb_game:                if (arg1) {                    Toast.makeText(MainActivity.this, "少玩游戏多写代码", 1000).show();                    cb_game.setTextColor(Color.RED);                } else {                    cb_game.setTextColor(Color.BLACK);                }                Toast.makeText(MainActivity.this, "电竞:" + arg1, 1000).show();                break;            case R.id.cb_travel:                Toast.makeText(MainActivity.this, "旅游:" + arg1, 1000).show();                break;            case R.id.cb_read:                Toast.makeText(MainActivity.this, "阅读:" + arg1, 1000).show();                break;            }        }    }    class ButtonListener implements OnClickListener {        @Override        public void onClick(View arg0) {            switch (arg0.getId()) {            case R.id.btn_all:                cb_game.setChecked(true);                cb_read.setChecked(true);                cb_travel.setChecked(true);                break;            case R.id.btn_notall:                cb_game.setChecked(false);                cb_read.setChecked(false);                cb_travel.setChecked(false);                break;            case R.id.btn_getResult:                if (cb_game.isChecked()) {                    lists.add(cb_game.getText().toString());                }                if (cb_read.isChecked()) {                    lists.add(cb_read.getText().toString());                }                if (cb_travel.isChecked()) {                    lists.add(cb_travel.getText().toString());                }                showResult.setText(lists.toString());                lists.clear();                break;            }        }    }}

在获取显示内容的时候我们先检查该选项是否被选中,若选中了则把text内容添加到List集合中,使用setText(lists.toString())方法为文本条添加内容。之后我们应该清空集合,以便下次使用。