單選框
1、實作效果
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLwMjYhNTM1IGOxkjM4Q2M4M2MlRTYkRTOyQTNkFGZiR2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2、布局
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你選擇的小說類型是:"/>
<RadioButton
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="幻想現純"/>
<RadioButton
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="架空曆史"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="無cp"/>
</RadioGroup>
<TextView
android:id="@+id/xianshi"
android:textSize="30dp"
android:textColor="@color/purple_200"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
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、布局
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你的老婆是"/>
<CheckBox
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="竈門祢豆子"/>
<CheckBox
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="百合絢鬥"/>
<CheckBox
android:id="@+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生志摩妄"/>
<CheckBox
android:id="@+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="白钰袖"/>
<CheckBox
android:id="@+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="宮野"/>
<CheckBox
android:id="@+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="西條高人"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"/>
</LinearLayout>
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);
}
}
}
}