页面代码
<?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;
}
}
}