安卓菜鸟的自学笔记——5:复选框的简单应用(入门)

Button btn_login;        //定义登录按钮
    CheckBox checkBox1, checkBox2, checkBox3;   //定义复选框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_login = (Button) findViewById(R.id.btn_login);      //通过ID获取布局确认登录按钮
        checkBox1 = (CheckBox) findViewById(R.id.checkbox1);    //通过ID获取布局复选框1
        checkBox2 = (CheckBox) findViewById(R.id.checkbox2);    //通过ID获取布局复选框2
        checkBox3 = (CheckBox) findViewById(R.id.checkbox3);    //通过ID获取布局复选框3
        btn_login.setOnClickListener(new View.OnClickListener() {   //为确认登录按钮
            @Override
            public void onClick(View v) {
                String checked = "";           //保存选中的值
                if (checkBox1.isChecked()) { //当第一个复选框被选中
                    checked += checkBox1.getText().toString() ;  //输出第一个复选框内信息
                }
                if (checkBox2.isChecked()) { //当第二个复选框被选中
                    checked += checkBox2.getText().toString() ;  //输出第二个复选框内信息
                }
                if (checkBox3.isChecked()) { //当第三个复选框被选中
                    checked += checkBox3.getText().toString() ;  //输出第三个复选框内信息
                }
                //显示被选中复选框对应的信息
                Toast.makeText(MainActivity.this, checked, Toast.LENGTH_LONG).show();
            }
        });
    }
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录后该应用将获得以下权限"
        android:textSize="14sp"
        />
    <CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获得你的公开信息(昵称、头像等)"
        android:checked="true"
        android:textSize="12sp"
        android:textColor="#BDBDBD"/>
    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="寻找与你共同使用该应用的好友"
        android:checked="true"
        android:textSize="12sp"
        android:textColor="#BDBDBD"/>
    <CheckBox
        android:id="@+id/checkbox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="帮助你通过该应用向好友发送消息"
        android:checked="true"
        android:textSize="12sp"
        android:textColor="#BDBDBD"/>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#009688"
        android:text="确认登录"/>
    <Button
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF" android:text="取消"/>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值