欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

安卓CheckBox实现单选

发布时间:2024/10/5 编程问答 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 安卓CheckBox实现单选 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

1.activity_main.xml:

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

<LinearLayoutandroid:id="@+id/layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="=>选择你的性别:"android:textColor="@color/purple_200" /><CheckBoxandroid:id="@+id/man"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="" /><CheckBoxandroid:id="@+id/woman"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="" /><TextViewandroid:id="@+id/msg"android:layout_width="match_parent"android:layout_height="match_parent" /><Buttonandroid: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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {man.setChecked(true);woman.setChecked(false);} else {man.setChecked(false);}}});woman.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic 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() {@Overridepublic void onClick(View view) {new AlertDialog.Builder(MainActivity.this).setTitle("提示信息").setMessage("确定选择?").setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic 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() {@Overridepublic 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 ;@Overrideprotected 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() {@Overridepublic void onClick(View view) {new AlertDialog.Builder(MainActivity.this).setTitle("提示信息").setMessage("确定选择?").setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {man.setChecked(true);woman.setChecked(false);} else {man.setChecked(false);}}});woman.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {woman.setChecked(true);man.setChecked(false);} else {woman.setChecked(false);}}});} }

最后效果图:gif:

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是生活随笔为你收集整理的安卓CheckBox实现单选的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。