当前位置: 移动技术网 > 移动技术>移动开发>Android > android的复选框(checkBox)的实际使用案例分享

android的复选框(checkBox)的实际使用案例分享

2020年08月11日  | 移动技术网移动技术  | 我要评论

运行效果


在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();
            }
        });
    }
} 

图片资源

本文地址:https://blog.csdn.net/qq_44716544/article/details/107913085

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网