Android:在Java中使用RadioButton.isChecked()方法

时间:2018-11-30 22:16:41

标签: java android xml android-studio radio-button

我正在尝试创建一个过滤列表的系统(在这种情况下,每个元素都是一个RelativeLayout元素)。我在XML代码中使用3个RadioButton元素,其ID为radio1,radio2,radio3,分别显示前3个,前5个和前10个元素。在我的Java类中,我尝试使用RadioButton.isChecked()方法创建列表,但是由于某些原因,条件从未满足。有人可以帮我吗?

private void populateList() { //
    RelativeLayout popList[] = new RelativeLayout[10];


    RadioButton radio1 = (RadioButton) findViewById(R.id.radio1); //top 3
    RadioButton radio2 = (RadioButton) findViewById(R.id.radio2); //top 5 
    RadioButton radio3 = (RadioButton) findViewById(R.id.radio3); //top 10

    //each represents an elemnent of the list
    popList[0] = (RelativeLayout) findViewById(R.id.populate_list1);
    popList[1] = (RelativeLayout) findViewById(R.id.populate_list2);
    popList[2] = (RelativeLayout) findViewById(R.id.populate_list3);
    popList[3] = (RelativeLayout) findViewById(R.id.populate_list4);
    popList[4] = (RelativeLayout) findViewById(R.id.populate_list5);
    popList[5] = (RelativeLayout) findViewById(R.id.populate_list6);
    popList[6] = (RelativeLayout) findViewById(R.id.populate_list7);
    popList[7] = (RelativeLayout) findViewById(R.id.populate_list8);
    popList[8] = (RelativeLayout) findViewById(R.id.populate_list9);
    popList[9] = (RelativeLayout) findViewById(R.id.populate_list10);

    //conditions
    if(radio1.isChecked()) {
        for(int i = 0; i < 3; i++) {
            popList[i].setVisibility(View.VISIBLE);
        }

        for(int i = 3; i < 10; i++) {
            popList[i].setVisibility(View.GONE);
        }
    }
    else if(radio2.isChecked()) {
        for(int i = 0; i < 5; i++) {
            popList[i].setVisibility(View.VISIBLE);
        }
        for(int i = 5; i < 10; i++) {
            popList[i].setVisibility(View.GONE);
        }
    }
    else if(radio3.isChecked()) {
        for(int i = 0; i < 10; i++) {
            popList[i].setVisibility(View.VISIBLE);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

如果三个单选按钮在单选组中,则可以使用getCheckedRadioButtonId

switch (your_radiogroup.getCheckedRadioButtonId()) {
    case R.id.radio1:
        //do something
        break;

    case R.id.radio2:
        //do something
        break;

    case R.id.radio3:
        //do something
        break;
    default:
        break;
}

答案 1 :(得分:0)

我认为您的代码段中有两个问题。

首先,如果您想要良好的RadioButtons行为,则必须在视图(check this to have more infos)中将它们分组在RadioGroup中。

此后,您可以添加一个侦听器以捕获更改了哪个按钮,如下所示:

// inflate from your view
RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);

myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.radio1:
                        // Do something cool
                        break;
                    case R.id.radio2:
                        // Do something cool too
                        break;
                    case R.id.radio3:
                        // Do something else
                        break;
                }
            }
        });

希望它能对您有所帮助。