activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView19"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多级及单选演示"
android:gravity="center"
android:textSize="34sp" />
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView19"
android:orientation="horizontal">
<TextView
android:id="@+id/textView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="性别:"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"
android:textSize="16sp" />
<RadioGroup
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
>
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textSize="16sp" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textSize="16sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_below="@+id/line1"
android:id="@+id/line2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textView23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓球" />
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" />
<CheckBox
android:id="@+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跑步" />
</LinearLayout>
<LinearLayout
android:id="@+id/line3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/line2"
android:orientation="horizontal">
<TextView
android:id="@+id/textView24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您选择了性别为:" />
<TextView
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_below="@+id/line3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="您选择了爱好为:" />
<TextView
android:id="@+id/editTextTextPersonName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:text="" />
</LinearLayout>
</RelativeLayout>
MainActivity
package com.example.layoutdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
RadioGroup rg;
RadioButton btn1;
RadioButton btn2;
TextView text1;
CheckBox checkBox1;
CheckBox checkBox2;
CheckBox checkBox3;
TextView text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg = findViewById(R.id.radio1);
btn1 = findViewById(R.id.radioButton3);
btn2 = findViewById(R.id.radioButton4);
text1 = findViewById(R.id.editTextTextPersonName);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.radioButton3:
text1.setText(btn1.getText());
break;
case R.id.radioButton4:
text1.setText(btn2.getText());
break;
}
}
});
checkBox1 = findViewById(R.id.checkBox4);
checkBox2 = findViewById(R.id.checkBox5);
checkBox3 = findViewById(R.id.checkBox6);
text2 = findViewById(R.id.editTextTextPersonName1);
View.OnClickListener clickListener = new View.OnClickListener(){
@Override
public void onClick(View view) {
if (checkBox1.isChecked() && checkBox2.isChecked() && checkBox3.isChecked()){
text2.setText(checkBox1.getText()+","+checkBox2.getText()+","+checkBox3.getText());
}else if (checkBox1.isChecked() && checkBox2.isChecked()){
text2.setText(checkBox1.getText()+","+checkBox2.getText());
}else if (checkBox2.isChecked() && checkBox3.isChecked()){
text2.setText(checkBox2.getText()+","+checkBox3.getText());
}else if (checkBox1.isChecked() && checkBox3.isChecked()){
text2.setText(checkBox1.getText()+","+checkBox3.getText());
}else if (checkBox1.isChecked()){
text2.setText(checkBox1.getText());
}else if (checkBox2.isChecked()){
text2.setText(checkBox2.getText());
}else if (checkBox3.isChecked()){
text2.setText(checkBox3.getText());
}
}
};
checkBox1.setOnClickListener(clickListener);
checkBox2.setOnClickListener(clickListener);
checkBox3.setOnClickListener(clickListener);
}
}