简体   繁体   English

Android-根据值获取单选按钮ID

[英]Android -get radio button id based on value

I want to highlight right and wrong option when user clicks a button after checking an radio button. 当用户在选中单选按钮后单击按钮时,我想突出显示对与错选项。 If that option is right, highlight. 如果该选项正确,则突出显示。 Else highlight right option. 否则突出显示正确的选项。

Is there any way to get radio button id in a group based on its value? 有什么方法可以根据其值在组中获取单选按钮ID? Or Do I need to use switch case? 还是我需要使用开关盒?

I searched enough but not able to find out what I need. 我进行了足够的搜索,但是找不到我需要的东西。

Edit 编辑

I have simple layout which contains one question, 4 choices, one button. 我的布局简单,其中包含一个问题,4个选择和一个按钮。 User check a radio button and click the check button. 用户检查单选按钮,然后单击检查按钮。 If user selects wrong option, highlight the correct option. 如果用户选择了错误的选项,请突出显示正确的选项。 I know what is correct option by value. 我知道按值正确的选择是什么。

choice1, choice2, choice3, choice4 are four radio buttons. choice1,choice2,choice3,choice4是四个单选按钮。 User checks choice3. 用户检查选择3。 But choice2 is correct. 但是choice2是正确的。 How do I select choice2 by value in this radio group. 如何在此单选组中按值选择choice2。

  group.getRadioButtonId("choice2")

Anything similar to this? 有什么类似的吗?

You could iterate through the children of your RadioGroup to get the required one: 您可以遍历RadioGroup以获得所需的子级:

int count = radioGroup.getChildCount();
for (int i = 0 ; i < count; i++) {
    RadioButton button = (RadioButton) radioGroup.getChildAt(i);
    if (button.getText().equals("choice2")) {
        int id = button.getId(); // the ID you're looking for
    }
}

You Can check like this 你可以这样检查

@Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

            int rbId = group.getCheckedRadioButtonId();
            RadioButton rb = (RadioButton) findViewById(rbId);
            switch (group.getId()) {
            case R.id.radiogroupID:

                if (rb.getText().toString().equalsIgnoreCase("right")) {
                    // your logic 
                } else {
                    //your logic
                }
            }
        }

you can use these line. 您可以使用这些行。 its working for me 它为我工作

 int checkedId = radioGroup.getCheckedRadioButtonId();
    View radioButton = radioGroup.findViewById(checkedId);
    int radioId = radioGroup.indexOfChild(radioButton);
    RadioButton btn = (RadioButton) radioGroup.getChildAt(radioId);

First check which option is true. 首先检查哪个选项是正确的。 hear choice 2 OK. 听到选择2,确定。 set by default is checked true and than check user checked value . 默认设置为true,然后检查用户的选中值。 if user select other option than display message and pass true option we already checked true 如果用户选择显示消息以外的其他选项并传递true选项,则我们已经选中true

Create an int array with all the ids of the radiobutton 使用单选按钮的所有ID创建一个int数组

int[] rb_IDs=new int[]{R.id.rb1,R.id.rb2,R.id.rb3};

Then inside your button click : 然后在按钮内单击:

RadioGroup rg=(RadioGroup) findViewById(R.id.rg);
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            for (int i=0;i<rb_IDs.length;i++)
            {
                RadioButton rb=(RadioButton)findViewById(rb_IDs[i]);
                if (rb.getText().toString().equals("YOUR CORRECT ANSWER")&&!rb.isChecked())
                {
                    //
                    //Do your thing
                    //

                    //ID of the correct answer Radiobutton is
                    int id=rb_IDs[i];

                    //HighLighting the answer
                    rb.setBackgroundColor(Color.parseColor("#00ff00"));

                    break;
                }
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM