简体   繁体   English

如何在Android中使用单选复选框创建AlertDialog?

[英]How to create AlertDialog with single choice checkbox in android?

How to implement single choice with checkboxes in AlertDialog? 如何使用AlertDialog中的复选框实现单选? I try to implement with setSingleChoiceItems , but it displays radio button. 我尝试使用setSingleChoiceItems实现,但它显示单选按钮。 setMultiChoiceItems displays checkboxes but there user can check multiple items. setMultiChoiceItems显示复选框,但是用户可以检查多个项目。 I need to implement singleChoice with Checkboxes. 我需要使用复选框实现singleChoice。 I need yr help? 我需要您的帮助吗?

Programmatically, with only two checkboxes, you can write a switch case or if-else loop with the onCheckChangedListener like; 以编程方式,只有两个复选框,您可以使用onCheckChangedListener类的方法编写一个switch例或if-else循环;

public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked){
switch(arg0.getId())
  {
    case R.id.cbOne:
         cbOne.setChecked(true);
         cbTwo.setChecked(false);
         break;
    case R.id.cbTwo:
         cbTwo.setChecked(true);
         cbOne.setChecked(false);
         break;

  }
}

And for both checkbox items you can set the listener as: 对于这两个复选框,您都可以将侦听器设置为:

yourCb = (CheckBox)findViewById(R.id.yourCb);
yourCb.setOnCheckedChangeListener(yourListener);

If you customise the background, the default behaviour would also suit your requirement.. custom-android-checkbox-radiobutton 如果自定义背景,则默认行为也将满足您的要求。custom-android-checkbox-radiobutton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM