1.LinearLayout视图页面
<?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:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1、请选择你的职业:"
android:textColor="@color/black" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="教师" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="学生" />
</RadioGroup>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2、请选择你的业余爱好:"
android:textColor="@color/black"/>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="弹钢琴" />
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打网球" />
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="摄影" />
<Space
android:layout_width="match_parent"
android:layout_height="20dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="3"
android:text="显示选择结果:"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="3"
android:text=""
android:textColor="@android:color/black"/>
<Space
android:layout_width="match_parent"
android:layout_height="20dp"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="确定" />
</LinearLayout>
2.入口代码 MainActivity.java
package com.ning.group;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//声明文本框
TextView textView2;
TextView textView4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化布局管理器
final LinearLayout mLyaout = (LinearLayout) findViewById(R.id.layout1);
//获取文本框
textView2 = (TextView) findViewById(R.id.textView2);
textView4 = (TextView) findViewById(R.id.textView4);
//获取复选框按钮
final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkbox3);
//获取单选框按钮
final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
//设置状态改变监听器
checkBox1.setOnCheckedChangeListener(checkBox_listener);
checkBox2.setOnCheckedChangeListener(checkBox_listener);
checkBox3.setOnCheckedChangeListener(checkBox_listener);
radioButton1.setOnCheckedChangeListener(radioButton_listener);
radioButton2.setOnCheckedChangeListener(radioButton_listener);
//显示选择结果
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringBuilder myHobby = new StringBuilder(); //选择结果
StringBuilder career = new StringBuilder();
myHobby.append("你的业余爱好是:\n");
if(checkBox1.isChecked()) //当第1个复选框被选中
myHobby.append(checkBox1.getText().toString()+" ");
if(checkBox2.isChecked()) //当第2个复选框被选中
myHobby.append(checkBox2.getText().toString()+" ");
if(checkBox3.isChecked()) //当第3个复选框被选中
myHobby.append(checkBox3.getText().toString()+" ");
//显示被选中的复选框的值
if(myHobby.length() == 1){
textView2.setText("未选择任何爱好。");
}else{
textView2.setText(myHobby);
}
career.append("你的职业是:\n");
if(radioButton1.isChecked())
career.append(radioButton1.getText().toString()+" ");
if(radioButton2.isChecked())
career.append(radioButton2.getText().toString()+" ");
//显示被选中的单选框的值
textView4.setText(career);
}
});
}
//创建一个状态改变监听对象
private CompoundButton.OnCheckedChangeListener checkBox_listener = new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
//判断复选框是否被选中
if(isChecked){
textView2.setText("你选中的业余爱好是:\n"+buttonView.getText().toString());
}else{
textView2.setText("复选框的选择被取消。");
}
}
};
private CompoundButton.OnCheckedChangeListener radioButton_listener = new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if(isChecked){
textView4.setText("你选中的职业是:\n"+buttonView.getText().toString());
}
}
};
}