检查是否已选中任何复选框

时间:2014-02-16 16:41:18

标签: android checkbox actionlistener dayofweek

我在我的Android应用程序中有工作日的复选框,我想让一个监听器检查是否检查了这些复选框中的任何一个,但我的方式似乎很难,是不是有办法将所有这些监听器收集到一个

 sun.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});

    mon.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
    tue.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
    wed.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
    thu.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
    fri.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
    sat.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});

5 个答案:

答案 0 :(得分:4)

您可以检查正在触发的buttonView的ID并进行正确的验证,您只需将相同的onCheckListener分配给复选框并执行以下操作:

private OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch(buttonView.getId()){
        case R.id.checkId1:
            //TODO: Code for checked one...
        break;
        case R.id.checkId2:
            //TODO: Code for checked two...
        break;
        }
    }
};

希望它有帮助!

问候!

答案 1 :(得分:1)

如果您具有此处未提供的类似逻辑,则可以创建一个复选框列表,并在一个循环中为它们创建侦听器,例如。

答案 2 :(得分:1)

让你的活动实现OnCheckedChangeListener然后你有onCheckChanged ......

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    switch (buttonView.getId()) {
    case R.id.sun:

        break;

    case R.id.mon:

        break;

    default:
        break;
    }

}

答案 3 :(得分:1)

有;使用他的类

中的方法实现View.OnClickListener
@Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.checbox1:

                      if(checkbo1.isCheck){
                         //do logic then dont forget to set to the opposite state
                           checkbox.setChecked(false);
                      }
                      else{
                           //do logic
                          checkbox.setChecked(true);
                      }
                      break;
//
            case R.id.checbox2:
                        //do logic etc...
                      break;


        }

    }

然后在用户

的不同点击事件之间使用switch case处理

希望有所帮助

答案 4 :(得分:1)

当然有一种更简单的方法。只需将你的Activity放在你正在做的地方,就可以实现OnCheckedChangeListener

public Class MyActivity extends Activity implements OnCheckedChangeListener{

    //your activity logic in here


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         //your Activity will now receive onCheckedChangeEvents here from
         //the checkboxes to which you assign it as a listener
    }

}

,在onCreate中,在您获得对所有日期复选框的引用后,将Activity设置为此类监听器

@Override
public void onCreate(Bundle savedInstanceState){
    //first get references to all your checkboxes and assign them to mon, tue, wed etc.
    //then set the listeners
    mon.setOnCheckedChangeListener(this);
    tue.setOnCheckedChangeListener(this);
    wed.setOnCheckedChangeListener(this);
    thu.setOnCheckedChangeListener(this);
    fri.setOnCheckedChangeListener(this);
    sat.setOnCheckedChangeListener(this);
    sun.setOnCheckedChangeListener(this);
}

,确保所有复选框都在布局xml中分配了ID,如下所示

<CheckBox
    android:id="@+id/checkMonday"
 ....

,那么你将有一个onCheckedChange方法,你可以像这样处理不同的日子

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    int id = buttonView.getId();
    if(isChecked){
        counter++; //a day has been checked
    }else{
        counter--; //a day has been unchecked
    }
    switch(id){
        case R.id.checkMonday:
             //logic for Monday
             break;

        case R.id.checkTuesday:
             //logic for Tuesday
             break;

        ...
    }
}

这应该可以做到!