安卓CheckBox实现单选

开发工具:Android studio
jdk:1.8
安卓谷歌12

1.activity_main.xml:

首先设置一个LinearLayout,里面添加子控件然后和另一个用来显示选择结果的,最后来个button。
效果预览图:在这里插入图片描述代码:

<LinearLayout
        android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="=>选择你的性别:"
            android:textColor="@color/purple_200" />

        <CheckBox
            android:id="@+id/man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="" />

        <CheckBox
            android:id="@+id/woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="" />

        <TextView
            android:id="@+id/msg"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="选择" />

    </LinearLayout>

2.MainActivity.java

先获取每一个控件

CheckBox man, woman;
Button button;
TextView msg ;

监听选择状态的方法:

private void initView() {
        man = (CheckBox) findViewById(R.id.man);
        woman = (CheckBox) findViewById(R.id.woman);
        man.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    man.setChecked(true);
                    woman.setChecked(false);
                } else {
                    man.setChecked(false);
                }
            }
        });
        woman.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    woman.setChecked(true);
                    man.setChecked(false);
                } else {
                    woman.setChecked(false);
                }
            }
        });

onCreate:button监听获取到checkbox

initView();

        final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

        button = (Button) findViewById(R.id.button);
        msg = (TextView)findViewById(R.id.msg);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("提示信息")
                        .setMessage("确定选择?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                StringBuilder sex = new StringBuilder();
                                sex.append("性别为:");
                                if (man.isChecked()) {
                                    sex.append(man.getText().toString() + " ");
                                } else if (woman.isChecked()) {
                                    sex.append(woman.getText().toString() + " ");
                                } else {
                                    msg.setText("未选择性别。\n");
                                }
                                msg.setText(sex);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this,"取消选择",Toast.LENGTH_LONG).show();
                            }
                        })
                        .create().show();
            }
        });

3.MainActivity.java全代码

package edu.zut.mysqltest;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
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.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    CheckBox man, woman;
    Button button;
    TextView msg ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

        button = (Button) findViewById(R.id.button);
        msg = (TextView)findViewById(R.id.msg);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("提示信息")
                        .setMessage("确定选择?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                StringBuilder sex = new StringBuilder();
                                sex.append("性别为:");
                                if (man.isChecked()) {
                                    sex.append(man.getText().toString() + " ");
                                } else if (woman.isChecked()) {
                                    sex.append(woman.getText().toString() + " ");
                                } else {
                                    msg.setText("未选择性别。\n");
                                }
                                msg.setText(sex);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this,"取消选择",Toast.LENGTH_LONG).show();
                            }
                        })
                        .create().show();
            }
        });
    }

    private void initView() {
        man = (CheckBox) findViewById(R.id.man);
        woman = (CheckBox) findViewById(R.id.woman);
        //监听事件
        man.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    man.setChecked(true);
                    woman.setChecked(false);
                } else {
                    man.setChecked(false);
                }
            }
        });
        woman.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    woman.setChecked(true);
                    man.setChecked(false);
                } else {
                    woman.setChecked(false);
                }
            }
        });

    }
}

最后效果图:gif:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

roydon_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值