1、实现效果
2、布局
3、java代码
package com.example.check_box_application;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup=findViewById(R.id.rg);
textView=findViewById(R.id.xianshi);
radioGroup.setOnCheckedChangeListener((radioGroup, i) -> {
if(i==R.id.one){
textView.setText("你选择了幻想现纯");
}else if(i==R.id.two){
textView.setText("你选择了架空历史");
}else{
textView.setText("你选择了无cp");
}
});
}
}
复选框
1、实现效果
2、布局
3、java代码
package com.example.check_box_application;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class Fu_xuan_Activity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private String los;
private TextView lo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fu_xuan_layout);
TextView tv=findViewById(R.id.tv);
CheckBox one=findViewById(R.id.one);
CheckBox two=findViewById(R.id.two);
CheckBox three=findViewById(R.id.three);
CheckBox four=findViewById(R.id.four);
CheckBox five=findViewById(R.id.five);
CheckBox six=findViewById(R.id.six);
one.setOnCheckedChangeListener(this);
two.setOnCheckedChangeListener(this);
three.setOnCheckedChangeListener(this);
four.setOnCheckedChangeListener(this);
five.setOnCheckedChangeListener(this);
six.setOnCheckedChangeListener(this);
lo=findViewById(R.id.tv);
los=new String();
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
String xuanz=compoundButton.getText().toString();
if(b){
if(!los.contains(xuanz)){
los=los+xuanz;
lo.setText(los);
}
}else{
if (los.contains(xuanz)){
los=los.replace(xuanz,"");
lo.setText(los);
}
}
}
}



