TableLayout of radiogroup(s) with respective label(s) aligned in android

时间秒杀一切 提交于 2019-12-06 05:46:29

You can simply solve this issue with implementing OnCheckedChangeListener

try below code it will work for you-

rb1 = (RadioButton) findViewById(R.id.rb1);
rb1.setOnCheckedChangeListener(quest1);
rb2 = (RadioButton) findViewById(R.id.rb1);
rb2.setOnCheckedChangeListener(quest1);
rb3 = (RadioButton) findViewById(R.id.rb1);
rb3.setOnCheckedChangeListener(quest2);
rb4 = (RadioButton) findViewById(R.id.rb1);
rb4.setOnCheckedChangeListener(quest2);

CompoundButton.OnCheckedChangeListener quest1 = new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.rb1:
                rb2.setChecked(false);
                break;

            case R.id.rb2:
                rb1.setChecked(false);
        }
    }
};

CompoundButton.OnCheckedChangeListener ques2= new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.rb3:
                rb4.setChecked(false);
                break;

            case R.id.rb4:
                rb3.setChecked(false);
        }
    }
};

Sometimes when you cannot compromise on design, you have to compromise on efficiency. You can achieve this by adding a simple common OnCheckedChangeListener to all radio buttons.

public CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {

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

        // As all radio buttons have this listener added,
        // add this check to avoid repetitive execution of code and avoid ripples

        if (isChecked) {
            View parent = (View) buttonView.getParent();
            RadioButton lastCheckedRadioButton = (RadioButton) parent.getTag();
            if (lastCheckedRadioButton != null) {
                lastCheckedRadioButton.setChecked(false);
            }
            parent.setTag(buttonView);
        }
    }
};

What is the worst part?
It's when you add listener to all radio buttons.
If you can think of more efficient way, use your own solution, else, use this snippet to add listeners to all radio buttons (based on your layout sample).

TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
View child;
TableRow tableRow;
// As the first line is headings, skip the first row
for (int i = 1; i < tableLayout.getChildCount(); i++) {
    child  = tableLayout.getChildAt(i);
    if (child instanceof  TableRow) {
        tableRow = (TableRow) child;
        // As the first line has labels, skip the first column
        for (int j = 1; j < tableRow.getChildCount(); j++) {
            child  = tableRow.getChildAt(j);
            if (child instanceof RadioButton) {
                ((RadioButton) child).setOnCheckedChangeListener(onCheckedChangeListener);
            }
        }
    }
}

NOTE: If you have used setTag method for some other purpose, you can use setTag(R.id.radioButton) and getTag(R.id.radioButton) to achieve the result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!