Android: использование метода RadioButton.isChecked () в Java - PullRequest
0 голосов
/ 01 декабря 2018

Я пытаюсь создать систему, которая фильтрует список (в этом случае каждый элемент является элементом RelativeLayout).Я использую 3 элемента RadioButton в своем XML-коде с идентификаторами 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 голосов
/ 01 декабря 2018

По моему мнению, в вашем фрагменте есть две проблемы.

Во-первых, если вы хотите хорошего поведения RadioButtons, вы должны сгруппировать их в 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;
                }
            }
        });

Надеюсь, это поможет вам.

0 голосов
/ 01 декабря 2018

Если три переключателя находятся в группе радио, вы можете использовать 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...