How to get the id of selected radio button in android?

前端 未结 7 1883
遇见更好的自我
遇见更好的自我 2020-12-17 03:23

I am working on quiz application in android. We have created Select.java page which displays the questions and options(with radio buttons) from sqlite datab

相关标签:
7条回答
  • 2020-12-17 03:55

    I know it's a old question but i don't see my answer in anywhere and i found it more simple than others..

    so here we go:

    int myRadioChecked;
    if(radioGroup.getCheckedRadioButtonId() == findViewById(R.id.YOUR_RADIO_BUTTON).getId()) {
      /**Do Stuff*/
      //ex.: myRadioChecked = 1;
    }
    
    0 讨论(0)
  • 2020-12-17 03:55
    final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.MyRadioGroup);
    
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    
        @Override
        public void onCheckedChanged(RadioGroup arg0, int arg1) {
            int selectedId = radioGroup.getCheckedRadioButtonId();
            Log.i("ID", String.valueOf(selectedId));
    
        }
    });
    
    0 讨论(0)
  • 2020-12-17 03:55

    In RadioGroup Class you can use method getCheckedRadioButtonId();

    RadioGroup rg = findViewById(R.id.radioGroup); rg.getCheckedRadioButtonId();

    Returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.

    0 讨论(0)
  • 2020-12-17 03:57

    Hmmm, just add one more member variable in UserBO to store selected answer.

    Class UserBO {
    
    private int userID;
    private String userName;
    private String question;
    private String option1;
    private String option2;
    private String option3;
    
    private int answerID;
    
    //create getter and setters for above member variables
    } 
    

    then within onclick listener of Adapter class, do like as following

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(RadioGroup radioGroup,
                    int radioButtonID) {
            switch(radioButtonID) {
                    case R.id.option1:
                           listItem.setAnswerID(1);
                break;
            case R.id.option2:
                    listItem.setAnswerID(2);
                break;
    
                     }
            }
        });
    

    then change your header constructor to receive userarraylist (which contains user details with answer)

    ArrayList<USerBO> userList;
    Header(Context context, AttributeSet attrs, ArrayList<UserBO> userALt) {
    userList = userAL;
    }
    
    //on next button click
    
    onclick() {
        for(UserBO userObj: userList) {
            if (userObj.getAnswerID != 0)
             Log.d("AnswerID", userObj.getAnswerID);
        }
    }
    

    it just like sudo code.. i hope this will help u..

    0 讨论(0)
  • 2020-12-17 03:59

    This is the best way:

    RadioButton button = findViewById(v.getId());
    
    0 讨论(0)
  • 2020-12-17 04:00

    you can get the id of selected button by the following this.Here

    int position = group.indexOfChild(radioButton);
    

    will give you the id.Also you can to Toast to see id like this

     Toast.makeText(MainActivity.this,"Id of radio button"+position+, Toast.LENGTH_SHORT).show();
    

    This will pop up - "Id of radio button you clicked is 0" if you clicked first button.

    0 讨论(0)
提交回复
热议问题