1.activity_main.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/group"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<RadioButton
android:id="@+id/java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="java" />
<RadioButton
android:id="@+id/c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="c++" />
</RadioGroup>
<TextView
android:id="@+id/show"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<CheckBox
android:id="@+id/box1"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="语文" />
<CheckBox
android:id="@+id/box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数学" />
<CheckBox
android:id="@+id/box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="英语" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/show2"
android:layout_marginTop="10dp"
/>
</LinearLayout>
2.MainActivity.java
package demo.com.danxuankuang;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioGroup group;
TextView show;
TextView show2;
CheckBox box1;
CheckBox box2;
CheckBox box3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
group = findViewById(R.id.group);
show = findViewById(R.id.show);
show2 = findViewById(R.id.show2);
box1 = findViewById(R.id.box1);
box2 = findViewById(R.id.box2);
box3 = findViewById(R.id.box3);
//复选框设置监听事件
CheckList checkList = new CheckList();
box1.setOnCheckedChangeListener(checkList);
box2.setOnCheckedChangeListener(checkList);
box3.setOnCheckedChangeListener(checkList);
//单选框设置监听事件
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.android:
show.setText("你选择了:Andrlid");
break;
case R.id.java:
show.setText("你选择了:Java");
break;
case R.id.c:
show.setText("你选择了:C++");
break;
default:
break;
}
}
});
}
//选中的回调函数
class CheckList implements CompoundButton.OnCheckedChangeListener {
StringBuilder sb = new StringBuilder();
String str;
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.box1:
str = "语文";
chage(str,isChecked);
show2.setText(sb.toString());
break;
case R.id.box2:
str = "数学";
chage(str,isChecked);
show2.setText(sb.toString());
break;
case R.id.box3:
str = "英语";
chage(str,isChecked);
show2.setText(sb.toString());
break;
default:
break;
}
}
/**
* 对选中按钮字符串的拼接和删除操作
* @param str 要操作的字符
* @param bb 判断是否选中
*/
private void chage(String str,boolean bb){
if(bb){
//字符串拼接
sb.append(str);
}else {
//字符串删除
int start = str.indexOf(str);
//如果字符串存在
if(start!=-1){
int end = str.length()+start;
sb.delete(start,end);
}
}
}
}
}