【问题标题】:How to check state Changing of CheckBox in android?如何在android中检查CheckBox的状态变化?
【发布时间】:2017-08-19 16:47:45
【问题描述】:

我想检查我的复选框是否被选中以更改某些值。 我得到了插入到 RadioGroup 的 radioButton 的答案,我们可以简单地使用:

radioGroup.setOnCheckedChangeListener(new new OnCheckedChangeListener(){
@Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    { . . . } 
}

我正在寻找这样的东西,但是这种方法与 Checkboxes 不同,因为没有像 CheckboxGroup 这样的小部件可以使用 setOnCheckedChangeListener 。

例如,我有 checkbox1 和 checkbox2。只要选中这两个复选框以更改某些值,我怎么能得到:

if(checkbox1==isChecked && checkbox2==isChecked)
//Do sth ...
else 
//Do sth Different

编辑:也许我不能完美地解释我的问题,这就像下面的问题,但对于复选框 How to check state Changing of Radio Button android?

【问题讨论】:

  • 你的意思是if(checkbox1.isChecked() && checkbox2.isChecked())
  • 是的,但我想在不同的功能中使用它们,可以全局访问此复选框@Pavneet_Singh

标签: android checkbox


【解决方案1】:

作为commented,你可以使用checkboxRef.isChecked()

if(checkbox1.isChecked() && checkbox2.isChecked()){

}
else{

}

但我想在不同的功能中使用它们,可以全局访问 到这个复选框

您可以使用sharedPreference 和实用程序类来存储复选框的状态。

SharedPreferences helper class

【讨论】:

    【解决方案2】:

    这是检查哪个复选框被选中的方法..

    if(checkbox1.isChecked())
    {
    //means checkbox1 is checked...
    }else
    {
    //means checkbox2 is checked...
    }
    

    【讨论】:

      【解决方案3】:

      如果您想设置一个事件监听器来检测何时进行更改,您可以使用:

      checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {     
      @Override
          public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
              //Code here
       } } ); 
      

      另一方面,如果你想检查它是否被检查,你可以使用:

      if(checkbox.isChecked()){
          //Code here
      }
      

      【讨论】:

        【解决方案4】:

        最后我得到了答案,首先我需要将所有复选框的 onclick 属性设置为相同,并在 onClick 函数中检查我的 Needed for approach 希望输出,

        XML:

          <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="func"
            android:text="1"
            android:id="@+id/c1"/>
        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2"
            android:onClick="func"
            android:id="@+id/c2"/>
        
        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="3"
            android:onClick="func"
            android:id="@+id/c3"/>
        

        Java代码:

        public void func(View view) {
        
        
            CheckBox checkBox1 = (CheckBox)findViewById(R.id.c1);
            CheckBox checkBox2 = (CheckBox)findViewById(R.id.c2);
            CheckBox checkBox3 = (CheckBox)findViewById(R.id.c3);
        
            if(checkBox1.isChecked() && checkBox2.isChecked() && (!checkBox3.isChecked()))
            {
                Next = true;
            }
            else
            {
                Next = false;
            }
        }
        

        【讨论】:

          【解决方案5】:
          checkBox1.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
          
                          if(checkBox1.isChecked() && checkBox2.isChecked()){
                          }
          
                      }
                  });
          

          【讨论】:

            猜你喜欢
            • 2016-04-20
            • 2018-01-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多