android多选和单选按钮

页面代码

<?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=".CBRBActivity">

   <RadioGroup
       android:id="@+id/RG"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
       <RadioButton
           android:id="@+id/RB1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="单选一"
           />
       <RadioButton
           android:id="@+id/RB2"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="单选二"
           />
       <RadioButton
           android:id="@+id/RB3"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="单选三"
           />

   </RadioGroup>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="----------------------------------分割线----------------------------------"
    />
    <CheckBox
        android:id="@+id/CB1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选一"
    />
    <CheckBox
        android:id="@+id/CB2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选二"
        />
    <CheckBox
        android:id="@+id/CB3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选三"
        />
    <Button
        android:id="@+id/bt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"
        />

</LinearLayout>

后台Java代码

package com.example.lenovo.myapplication;

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.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class CBRBActivity extends AppCompatActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{
RadioGroup RG;
RadioButton RB1,RB2,RB3;
CheckBox CB1,CB2,CB3;
Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cbrb);
        RG=findViewById(R.id.RG);
        RB1=findViewById(R.id.RB1);
        RB2=findViewById(R.id.RB2);
        RB3=findViewById(R.id.RB3);
        CB1=findViewById(R.id.CB1);
        CB2=findViewById(R.id.CB2);
        CB3=findViewById(R.id.CB3);
        bt=findViewById(R.id.bt);


        bt.setOnClickListener(this);
        RG.setOnCheckedChangeListener(this);

    }

    @Override
    public void onClick(View v) {
        String str="";
        if(!CB1.isChecked()&& !CB3.isChecked() && !CB2.isChecked() ){
            str="至少选一个";

        }
        if(CB1.isChecked()){
        str+=CB1.getText().toString();
        }
        if(CB2.isChecked()){
            str+=CB2.getText().toString();
        }
        if(CB3.isChecked()){
            str+=CB3.getText().toString();
        }
        Toast.makeText(this, "male"+str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
    switch (checkedId){
        case R.id.RB1:

            Toast.makeText(this, "male"+RB1.getText().toString(), Toast.LENGTH_SHORT).show();
              break;
        case R.id.RB2:
            Toast.makeText(this, "male"+RB2.getText().toString(), Toast.LENGTH_SHORT).show();
             break;
        case R.id.RB3:
            Toast.makeText(this, "male"+RB3.getText().toString(), Toast.LENGTH_SHORT).show();
            break;


    }
    }
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值