微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐