Android CheckBox的基本使用

Android CheckBox的基本使用

一、 说明
本文主要讲一下Android的CheckBox基本知识。
二、 所用工具
Android Studio
三、 具体内容

  1. CheckBox
    复选框,可以同时选中多个值
  2. 例子
    xml代码:
<LinearLayout
    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"
    android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择喜欢的水果:"
        android:textSize="23dp"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cb_banana"
        android:text="香蕉"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cb_apple"
        android:text="苹果"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cb_snowPear"
        android:text="雪梨"/>
       
</LinearLayout>
    <Button
        android:id="@+id/btn_Submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="提交"/>
</LinearLayout>

效果图:
在这里插入图片描述
Activity代码:

public class MainActivity extends AppCompatActivity {

    private CheckBox cbBanana;
    private CheckBox cbApple;
    private CheckBox cbSnowPear;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cbBanana = findViewById(R.id.cb_banana);
        cbApple = findViewById(R.id.cb_apple);
        cbSnowPear = findViewById(R.id.cb_snowPear);

        Button btnSubmit  = findViewById(R.id.btn_Submit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text ="";
                if(!cbBanana.isChecked()&&!cbApple.isChecked()&&!cbSnowPear.isChecked()){
                    Toast.makeText(MainActivity.this,"你未进行选择",Toast.LENGTH_LONG).show();
                }
                else {
                    if(cbBanana.isChecked()){
                        text+= cbBanana.getText().toString();
                    }
                    if(cbApple.isChecked()){
                        text+=cbApple.getText().toString();
                    }
                    if(cbSnowPear.isChecked()){
                        text+=cbSnowPear.getText().toString();

                    }
                    Toast.makeText(MainActivity.this,"你选择了"+text,Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

运行效果图:
在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值