安卓CheckBox实现单选

安卓CheckBox实现单选,第1张

开发工具: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:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/992581.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存