运行结果
activity_main.xml
使用单选按钮时,要把单选按钮RadioButton放入RadioGroup里,如图
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextText"
android:layout_width="376dp"
android:layout_height="47dp"
android:ems="10"
android:hint="输入姓名"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.086"
tools:layout_editor_absoluteX="1dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="73dp"
android:layout_height="21dp"
android:background="#3CAEEA00"
android:text="专业技能表"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.022" />
<TextView
android:id="@+id/textView"
android:layout_width="212dp"
android:layout_height="30dp"
android:text="1.所在专业"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.073"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.169" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="409dp"
android:layout_height="208dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26999998">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="356dp"
android:layout_height="wrap_content"
android:text="计算机科学学与技术" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="软件工程" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="网络工程" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="人工智能" />
</RadioGroup>
<TextView
android:id="@+id/textView4"
android:layout_width="212dp"
android:layout_height="30dp"
android:text="2.专业技能"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.07"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<CheckBox
android:id="@+id/tcp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TCP/IP协议"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.052"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.576" />
<CheckBox
android:id="@+id/spring"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spring架构"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.53"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.576" />
<CheckBox
android:id="@+id/jdbc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JDBC编程"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.93"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.576" />
<CheckBox
android:id="@+id/linux"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Linux基本使用"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.927"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.683" />
<CheckBox
android:id="@+id/socket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Socket编程"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.683" />
<CheckBox
android:id="@+id/jvm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JVM调优"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.509"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.683" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private CheckBox tcp_checkBox;
private CheckBox spring_checkBox;
private CheckBox jdbc_checkBox;
private CheckBox linux_checkBox;
private CheckBox socket_checkBox;
private CheckBox jvm_checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup=(RadioGroup) findViewById(R.id.radioGroup);
tcp_checkBox=(CheckBox)findViewById(R.id.tcp);
spring_checkBox=(CheckBox)findViewById(R.id.spring);
jdbc_checkBox=(CheckBox)findViewById(R.id.jdbc);
linux_checkBox=(CheckBox)findViewById(R.id.linux);
socket_checkBox=(CheckBox)findViewById(R.id.socket);
jvm_checkBox=(CheckBox)findViewById(R.id.jvm);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton=(RadioButton) findViewById(i);
Toast.makeText(getApplicationContext(),"已选择"+radioButton.getText(),Toast.LENGTH_LONG).show();
}
});
tcp_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked())
Toast.makeText(getApplicationContext(),"TCP/IP协议",Toast.LENGTH_LONG).show();
}
});
spring_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked())
Toast.makeText(getApplicationContext(),"Spring架构",Toast.LENGTH_LONG).show();
}
});
jdbc_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked())
Toast.makeText(getApplicationContext(),"JDBC编程",Toast.LENGTH_LONG).show();
}
});
linux_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked())
Toast.makeText(getApplicationContext(),"Linux基本使用",Toast.LENGTH_LONG).show();
}
});
socket_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked())
Toast.makeText(getApplicationContext(),"Socket编程",Toast.LENGTH_LONG).show();
}
});
jvm_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(compoundButton.isChecked())
Toast.makeText(getApplicationContext(),"JVM调优",Toast.LENGTH_LONG).show();
}
});
}
}