在Android上进行3次选择后禁用其余的radiobuttons

时间:2014-04-03 02:57:38

标签: android radio-button

我有7个radiobuttons作为选择的民意调查。我怎么做才能让他们选择之后呢 3个radiobuttons,无法选择和点击其余的radiobutton选项?或者这种方法有更简单的方法吗?感谢。

// Event handler for radio buttons
public void onRadioButtonClicked(View view) {

    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();





    // Check which radio button was clicked
    switch (view.getId()) {

    case R.id.RadioButton1:
        if (checked) {
            Toast.makeText(this, "You've selected: Hindi",
                    Toast.LENGTH_LONG).show();

        }

        break;
    case R.id.RadioButton2:
        if (checked)
            Toast.makeText(this, "You've selected: English",
                    Toast.LENGTH_LONG).show();

        break;
    case R.id.RadioButton3:
        if (checked) {
            Toast.makeText(this, "You've selected: English",
                    Toast.LENGTH_LONG).show();
            numThree++;
        }
        break;
    case R.id.RadioButton4:
        if (checked) {
            Toast.makeText(this, "You've selected: English",
                    Toast.LENGTH_LONG).show();

        }
        break;
    case R.id.RadioButton6:
        if (checked) {
            Toast.makeText(this, "You've selected: English",

        }
        break;
    case R.id.RadioButton7:
        if (checked) {
            Toast.makeText(this, "You've selected: English",
                    Toast.LENGTH_LONG).show();

        }
        break;
    }
}
}

3 个答案:

答案 0 :(得分:0)

我建议使用复选框,因为单选按钮通常意味着单个选项选择。

最初可以有一个0的计数器,当选中复选框时,它会增加1,而当未选中时,减1。计数器为3后,您将禁用所有未选中的选项。

答案 1 :(得分:0)

单选按钮最适合一次只能选择一个选项的情况。我建议使用复选框,例如:

public void onRadioButtonClicked(View view) {

int count = 0;
int maxSelected = 3;

// .....

case R.id.RadioButton1:
    if (checked) {
        Toast.makeText(this, "You've selected: Hindi",
                Toast.LENGTH_LONG).show();

               //LIKE THIS 
                count += 1;
                if(count>maxSelected) {
                     disableCheckboxes(); //this would move through and disable any checkbox that wasn't selected
                }

    }

答案 2 :(得分:0)

没有最简单的方法来解决您的问题。如果你真的想使用单选按钮,你必须使用没有单选按钮组的单选按钮。单击单选按钮应手动完成。通过选中单击​​单选按钮,您必须增加或减少单选按钮上的总数。 我只向你展示两个按钮。你必须完成其余的工作。

RadioButton radio1;
RadioButton radio2;
static int totalselectedRadioButton=0;

radio1.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View view) {

            boolean isChecked = radio1.isSelected();
// on radio button
            if (!isChecked) {
                radio1.setSelected(true);
                radio1.setChecked(true);
                totalselectedRadioButton ++;
//disable other button when reached expected total Selected radio button
                if(totalselectedRadioButton>0) {
                    radio2.setClickable(false);
                }
            }
            else {
                radio1.setSelected(false);
                radio1.setChecked(false);
                totalselectedRadioButton --;
                if(totalselectedRadioButton<1) {
                    radio2.setClickable(true);
                }
            }

        }
    });