Android Studio 之 CheckBox

 

•任务

  

•基本用法

  CheckBox,复选框,即可以同时选中多个选项。

      

  从网上找了三个图标,分别命名为 apple.jpg , banana.jpg , oranges.jpg 放置在了 drawable 文件夹下。

  实现代码如下:

<?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"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选出你最喜欢的水果"
        android:textColor="#000000"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:drawableRight="@drawable/apple" />

    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:drawableRight="@drawable/oranges" />
    
    <CheckBox
        android:id="@+id/cb_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:drawableRight="@drawable/banana" />
    

    <Button
        android:id="@+id/btn_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:textColor="#000000"
        android:textSize="20sp" />

</LinearLayout>

 

•自定义点击效果

  实现代码如下:

public class CheckBoxActivity extends AppCompatActivity {

    private CheckBox cb1;
    private CheckBox cb2;
    private CheckBox cb3;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);

        cb1 = findViewById(R.id.cb_1);//苹果
        cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(CheckBoxActivity.this, "apple", Toast.LENGTH_SHORT).show();
                }
            }
        });

        cb2 = findViewById(R.id.cb_2);//橘子
        cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(CheckBoxActivity.this, "orangens", Toast.LENGTH_SHORT).show();
                }
            }
        });

        cb3 = findViewById(R.id.cb_3);//香蕉
        cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(CheckBoxActivity.this, "banana", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn = findViewById(R.id.btn_cb);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String choose = "";
                if (cb1.isChecked() || cb2.isChecked() || cb3.isChecked())
                    choose += "你最喜欢的水果是:";
                else
                    choose += "都不喜欢!";

                if (cb1.isChecked())
                    choose += "apple";
                if (cb2.isChecked())
                    choose += ",orangens";
                if (cb3.isChecked())
                    choose += ",banana";
                Toast.makeText(CheckBoxActivity.this, choose, Toast.LENGTH_SHORT).show();
            }
        });

    }
}

  以上就是本次任务的实现代码。

  如果可供选择的水果有好多个,那么为每个水果都设置一个点击事件略微显得有点臃肿,如何精简一下呢?

•精简Java代码

public class CheckBoxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private CheckBox cb1;
    private CheckBox cb2;
    private CheckBox cb3;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);

        cb1 = findViewById(R.id.cb_1);//苹果
        cb1.setOnCheckedChangeListener(this);

        cb2 = findViewById(R.id.cb_2);//橘子
        cb2.setOnCheckedChangeListener(this);

        cb3 = findViewById(R.id.cb_3);//香蕉
        cb3.setOnCheckedChangeListener(this);

        btn = findViewById(R.id.btn_cb);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String choose = "";
                if (cb1.isChecked() || cb2.isChecked() || cb3.isChecked())
                    choose += "你最喜欢的水果是:";
                else
                    choose += "都不喜欢!";

                if (cb1.isChecked())
                    choose += "apple";
                if (cb2.isChecked())
                    choose += ",orangens";
                if (cb3.isChecked())
                    choose += ",banana";
                Toast.makeText(CheckBoxActivity.this, choose, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String s = "";
        switch (buttonView.getId()) {
            case R.id.cb_1:
                s += "apple";
                break;
            case R.id.cb_2:
                s += "orangens";
                break;
            case R.id.cb_3:
                s += "banana";
                break;
        }
        Toast.makeText(CheckBoxActivity.this, s, Toast.LENGTH_SHORT).show();

    }
}

  cb1.setOnCheckedChangeListener(this); : 括号中直接使用了 this,前提是:

  • 实现了 onCheckedChanged(CompoundButton buttonView, boolean isChecked) 方法
  • CheckBoxActivity implements CompoundButton.OnCheckedChangeListene

posted @ 2021-01-23 16:03  MElephant  阅读(595)  评论(0编辑  收藏  举报