android的复选框(checkBox)的案例

运行效果

 

在xml添加以下代码

<?xml version="1.0" encoding="utf-8"?>
<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"

    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="377dp"
        android:src="@mipmap/login4" />

    <TextView
        android:layout_width="221dp"
        android:layout_height="45dp"
        android:text="登录后该应用将获得以下权限"
        android:textSize="14sp" />
    <CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="221dp"
        android:layout_height="45dp"
        android:text="获得你的公开信息(昵称,头像等)"
        android:textColor="#BDBDBD"
        android:textSize="12sp"
        android:checked="true"
        />

    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="221dp"
        android:layout_height="45dp"
        android:checked="true"
        android:text="寻找与你共同使用该应用的好友"
        android:textColor="#BDBDBD"
        android:textSize="12sp" />

    <CheckBox
        android:id="@+id/checkbox3"
        android:layout_width="221dp"
        android:layout_height="45dp"
        android:checked="true"
        android:text="帮助你通过该应用向好友发送信息"
        android:textColor="#BDBDBD"
        android:textSize="12sp" />
    <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="取消"/>

</LinearLayout>

在Java文件添加以下代码

package com.example.wangzhelogin3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   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);
        checkBox1=(CheckBox)findViewById(R.id.checkbox1);
        checkBox2=(CheckBox)findViewById(R.id.checkbox2);
        checkBox3=(CheckBox)findViewById(R.id.checkbox3);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                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();
            }
        });
    }
}

图片资源

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值