Android循环复选框和togglebutton对

时间:2016-11-21 17:52:21

标签: android checkbox

我有20个复选框和togglebutton对(cb1,tb1,cb2,tb2,.....,cb20,tb20)。点击其中一个控件后,我想查看两个控件是否在检查对并进行相应的操作。以下onCreate代码是繁琐的方法:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ck1 = (CheckBox) findViewById(R.id.ck1);
    ck2 = (CheckBox) findViewById(R.id.ck2);
    tb1 = (ToggleButton) findViewById(R.id.tb1);
    tb2 = (ToggleButton) findViewById(R.id.tb2);

    ck1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (ck1.isChecked() & tb1.isChecked())  {
                //DO SOMETHING
            };
        }
    });

    ck2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (ck2.isChecked() & tb2.isChecked())  {
                //DO SOMETHING
            };
        }
    });

    tb1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (ck1.isChecked() & tb1.isChecked())  {
                //DO SOMETHING
            };
        }
    });

    tb2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (ck2.isChecked() & tb2.isChecked())  {
                //DO SOMETHING
            };
        }
    });
    //etc.etc.etc.
}

如何通过20对简化“循环”?

1 个答案:

答案 0 :(得分:0)

使用两个数组来存储ID,让Activity实现View.OnCLickListener

public class MyActivity extends Activity implements View.OnClickListener {

    private int[] checkboxes = {R.id.cb1, R.id.cb2, R.id.cb3};
    private int[] togglebuttons = {R.id.tb1, R.id.tb2, R.id.tb3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);    

        int len = checkboxes.length;
        for (int i = 0; i < len; i++)
        { 
            CheckBox ck = (CheckBox)findViewById(checkboxes[i]);
            ck.setOnClickListener(this);
            ToggleButton tb = (ToggleButton)findViewById(togglebuttons[i]);
            tb.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        int position = -1;
        if (v instanceof CheckBox)
        {
            CheckBox ck = (CheckBox)v;
            if (! ck.isChecked())
                return;

            position = findPosition(v.getId(), checkboxes);

            ToggleButton tb = (ToggleButton)findViewById(togglebuttons[position]);
            if (tb.isChecked())
            {
                doSomething(position);
            }
        }
        else if (v instanceof ToggleButton)
        {
            ToggleButton tb = (ToggleButton)v;
            if (! tb.isChecked())
                return;

            position = findPosition(v.getId(), togglebuttons);

            CheckBox ck = (CheckBox)findViewById(checkboxes[position]);
            if (ck.isChecked())
            {
                doSomething(position);
            }
        }
    }

    private int findPosition(int id, int[] arrayOfIds) {
        int len = arrayOfIds.length;
        for(int i = 0; i < len; i++)
        {
            if (id == arrayOfIds[i])
            { 
               return i;
            }
        }
        return -1; // should not happen
    }

    private void doSomething(int position) {
       // business logic here
    }
}