【问题标题】:Android: Using RadioButton.isChecked() method in JavaAndroid:在 Java 中使用 RadioButton.isChecked() 方法
【发布时间】:2019-05-03 02:06:53
【问题描述】:

我正在尝试创建一个过滤列表的系统(在这种情况下,每个元素都是一个 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);
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    如果三个单选按钮在一个单选组中,您可以使用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;
    }
    

    【讨论】:

    • @ShoaibAhmed 不客气,你能接受或支持我的回答吗,谢谢!
    【解决方案2】:

    在我看来,你的 sn-p 有两个问题。

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

    在这之后,你可以添加一个监听器来捕捉哪个按钮发生了变化,像这样:

    // 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;
                    }
                }
            });
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 2011-06-21
      • 2013-05-14
      相关资源
      最近更新 更多