1.效果 2.布局代码
<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="上海" />
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="确认"
android:textColor="#03A9F4"
android:textSize="20sp" />
<TextView
android:id="@+id/value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="籍贯"
android:textColor="#03A9F4"
android:textSize="50sp" />
自定义样式
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox4"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/checkbox_shape"
android:button="@null"
android:text="" />
<TextView
android:id="@+id/fruit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="苹果" />
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox5"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/checkbox_shape"
android:button="@null"
android:text="" />
<TextView
android:id="@+id/fruit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="香蕉" />
</LinearLayout>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox6"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/checkbox_shape"
android:button="@null"
android:text="" />
<TextView
android:id="@+id/fruit3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="葡萄" />
</LinearLayout>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="确认"
android:textColor="#03A9F4"
android:textSize="20sp" />
<TextView
android:id="@+id/fruit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="水果"
android:textColor="#673AB7"
android:textSize="50sp" />
checkbox_shape
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/checkbox_c" android:state_checked="true" />
<item android:drawable="@mipmap/checkbox_n" />
</selector>
3.java代码
package com.jsyj.android_p1.model;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import com.cmmboy.toastlib.ToastUtils;
import com.jsyj.android_p1.cmm.BaseActivity;
import com.jsyj.android_p1.databinding.ActivityCheckboxBinding;
import com.jsyj.android_p1.databinding.ActivityRadioBinding;
public class CheckboxActivity extends BaseActivity {
private ActivityCheckboxBinding binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityCheckboxBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
initClick();
}
private void initClick() {
binding.checkbox1.setChecked(true);
binding.checkbox4.setChecked(true);
binding.submit.setOnClickListener(v -> {
String text = "";
String text2 = "";
if (!binding.checkbox1.isChecked() && !binding.checkbox2.isChecked() && !binding.checkbox3.isChecked()) {
ToastUtils.info(CheckboxActivity.this, "你未进行选择籍贯");
} else {
if (binding.checkbox1.isChecked()) {
text += binding.checkbox1.getText().toString();
}
if (binding.checkbox2.isChecked()) {
text += binding.checkbox2.getText().toString();
}
if (binding.checkbox3.isChecked()) {
text += binding.checkbox3.getText().toString();
}
}
binding.value.setText(text);
if (!binding.checkbox4.isChecked() && !binding.checkbox5.isChecked() && !binding.checkbox6.isChecked()) {
ToastUtils.info(CheckboxActivity.this, "你未进行选择水果");
} else {
if (binding.checkbox4.isChecked()) {
text2 += binding.fruit1.getText().toString();
}
if (binding.checkbox5.isChecked()) {
text2 += binding.fruit2.getText().toString();
}
if (binding.checkbox6.isChecked()) {
text2 += binding.fruit3.getText().toString();
}
}
binding.fruit.setText(text2);
});
}
}
|