2

我的快速警报对话框中有 4 个复选框...我想要的是,当我选中 [启用] 1 个复选框时,其他 3 个复选框必须禁用。我点击它们,但没有禁用它们。我不知道该怎么做。

这是我正在寻找的:

小样

    checkmrn  =  (CheckBox) container.findViewById(R.id.MrnCheckBox);
    checkmrn.setText("By Mrn");
    checkmrn.setEnabled(false);
    checkdate  =  (CheckBox) container.findViewById(R.id.dateCheckBox);
    checkdate.setText("By Date");
    checkname  =  (CheckBox) container.findViewById(R.id.nameCheckBox);
    checkname.setText("By Name");
    checklocation  =  (CheckBox) container.findViewById(R.id.locationCheckBox);
    checklocation.setText("By Location");
    okButton = (Button) container.findViewById(R.id.okButton);
    okButton.setText("Ok");

    checkmrn.setOnClickListener(new OnClickListener() {          
          @Override
          public void onClick(View v) {                  
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(mContext, "By Mrn", Toast.LENGTH_SHORT).show();
            }        
          }
        });
    checkdate.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                  
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(mContext, "By Date", Toast.LENGTH_SHORT).show();
            }        
          }
        });
    checkname.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                  
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(mContext, "By Name", Toast.LENGTH_SHORT).show();
            }        
          }
        });
    checklocation.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                  
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(mContext, "By Location", Toast.LENGTH_SHORT).show();
            }        
          }
        });
4

7 回答 7

5

这是上述问题的工作代码。

  checkmrn  =  (CheckBox) container.findViewById(R.id.MrnCheckBox);
    checkmrn.setText("By Mrn"); 
    checkdate  =  (CheckBox) container.findViewById(R.id.dateCheckBox);
    checkdate.setText("By Date");   
    checkname  =  (CheckBox) container.findViewById(R.id.nameCheckBox);
    checkname.setText("By Name");
    checklocation  =  (CheckBox) container.findViewById(R.id.locationCheckBox);
    checklocation.setText("By Location");

    okButton = (Button) container.findViewById(R.id.okButton);
    okButton.setText("Ok");

    checkmrn.setOnClickListener(new OnClickListener() {          
          @Override
          public void onClick(View v) {                  

              if (((CheckBox) v).isChecked()) {
                  checkmrn.setChecked(true);
                  Toast.makeText(mContext, "By Mrn", Toast.LENGTH_SHORT).show();
                  checkdate.setChecked(false);
                  checkname.setChecked(false);
                  checklocation.setChecked(false);
                    } 
          }
        });
    checkdate.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                   
              if (((CheckBox) v).isChecked()) {
                  checkdate.setChecked(true);
                  Toast.makeText(mContext, "By Date", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkname.setChecked(false);
                  checklocation.setChecked(false);
                    } 
          }
        });
    checkname.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                                     
              if (((CheckBox) v).isChecked()) {
                  checkname.setChecked(true);
                  Toast.makeText(mContext, "By Name", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkdate.setChecked(false);
                  checklocation.setChecked(false);
                    }  
          }
        });
    checklocation.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                  

              if (((CheckBox) v).isChecked()) {
                  checklocation.setChecked(true);
                  Toast.makeText(mContext, "By Location", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkname.setChecked(false);
                  checkdate.setChecked(false);
                    } 
          }
        });
checkmrn  =  (CheckBox) container.findViewById(R.id.MrnCheckBox);
    checkmrn.setText("By Mrn"); 
    checkdate  =  (CheckBox) container.findViewById(R.id.dateCheckBox);
    checkdate.setText("By Date");   
    checkname  =  (CheckBox) container.findViewById(R.id.nameCheckBox);
    checkname.setText("By Name");
    checklocation  =  (CheckBox) container.findViewById(R.id.locationCheckBox);
    checklocation.setText("By Location");

    okButton = (Button) container.findViewById(R.id.okButton);
    okButton.setText("Ok");

    checkmrn.setOnClickListener(new OnClickListener() {          
          @Override
          public void onClick(View v) {                  

              if (((CheckBox) v).isChecked()) {
                  checkmrn.setChecked(true);
                  Toast.makeText(mContext, "By Mrn", Toast.LENGTH_SHORT).show();
                  checkdate.setChecked(false);
                  checkname.setChecked(false);
                  checklocation.setChecked(false);
                    } 
          }
        });
    checkdate.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                   
              if (((CheckBox) v).isChecked()) {
                  checkdate.setChecked(true);
                  Toast.makeText(mContext, "By Date", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkname.setChecked(false);
                  checklocation.setChecked(false);
                    } 
          }
        });
    checkname.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                                     
              if (((CheckBox) v).isChecked()) {
                  checkname.setChecked(true);
                  Toast.makeText(mContext, "By Name", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkdate.setChecked(false);
                  checklocation.setChecked(false);
                    }  
          }
        });
    checklocation.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                  

              if (((CheckBox) v).isChecked()) {
                  checklocation.setChecked(true);
                  Toast.makeText(mContext, "By Location", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkname.setChecked(false);
                  checkdate.setChecked(false);
                    } 
          }
        });
于 2012-09-14T11:11:33.147 回答
0

如果您想在选择一个选项时禁用其他选项,请创建禁用/启用方法

protected void setGroupState(boolean state)
{
     //here put all options to have state changed in one go
}

然后在应该更改“组”状态的选项的单击处理程序中

以适当的状态调用它。例如 setGroupState(false);

此致

于 2012-09-14T10:16:23.813 回答
0

您可以使用checkBoxwhich is的属性setClickable

以下是一个简单的代码示例,它允许禁用复选框:

CheckBox check_1;
check_1 = (CheckBoX)findViewById(R.id.(the id of your checkbox in xml));
check_1.setClickable(false);
//then this checkbox will be disabled.
于 2013-11-28T06:49:21.403 回答
0

单击复选框时,您可以使其他复选框为假,当前为真,例如

           if (((CheckBox) v).isChecked()) {
            currentchk.setChecked(true);
             otherchkbox.setChecked(false);
            }        
于 2012-09-14T10:34:48.150 回答
0

试试这样:

private OnCheckedChangeListener listener = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked){
switch(arg0.getId())
  {
    case R.id.firstCheckBox:
         firstCheckBox.setChecked(true);
         secondCheckBox.setChecked(false);
         thirdCheckBox.setChecked(false);
         break;
    case R.id.secondCheckBox:
         firstCheckBox.setChecked(false);
         secondCheckBox.setChecked(true);
         thirdCheckBox.setChecked(false);
         break;
   case R.id.thirdCheckBox:
        firstCheckBox.setChecked(false);
        secondCheckBox.setChecked(false);
       thirdCheckBox.setChecked(true);
        break;
  }
}

}
};
于 2012-09-14T10:37:41.343 回答
0

只需在 RadioGroup 中使用 RadioButton !!!这就是你想要的!

于 2012-09-14T11:51:58.000 回答
0

当您单击其他布局或按钮等时,您可以添加此行代码 ->

  1. 科特林: yourCheckbox.isChecked = !yourCheckbox.isChecked

  2. Java 中: yourCheckbox.setChecked() = !yourCheckbox.isChecked()

希望这可以帮助你:)

于 2018-01-19T10:25:03.873 回答