如何在android中启用或禁用复选框

时间:2012-09-14 10:11:54

标签: android android-layout checkbox

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

以下是我要找的内容:

mockup

    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();
            }        
          }
        });

7 个答案:

答案 0 :(得分: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);
                    } 
          }
        });

答案 1 :(得分:1)

只需在RadioGroup中使用单选按钮!这就是你想要的!

答案 2 :(得分:0)

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

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

然后在应该更改“组”

状态的选项的单击处理程序中

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

最好的问候

答案 3 :(得分:0)

单击Checkbox时,您可以将其他复选框设为false,当前状态为

           if (((CheckBox) v).isChecked()) {
            currentchk.setChecked(true);
             otherchkbox.setChecked(false);
            }        

答案 4 :(得分: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;
  }
}

}
};

答案 5 :(得分:0)

Well, you can use the property of the checkBox which is setClickable
Following is a simple example code which let a checkbox be disabled:

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.

Hope this will help you.

答案 6 :(得分:0)

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

    Kotlin: yourCheckbox.isChecked = !yourCheckbox.isChecked

    中的
  1. Java: yourCheckbox.setChecked() = !yourCheckbox.isChecked()

  2. 中的
  3. 希望这可以帮助你:)