How to check if a radiobutton is checked in a radiogroup in Android?

后端 未结 10 2140

I need to set validation that user must fill / select all details in a page. If any fields are empty wanna show Toast message to fill. Now I need set validation

10条回答
  •  Happy的楠姐
    2020-12-04 13:19

    I used the id's of my radiobuttons to compare with the id of the checkedRadioButton that I got using mRadioGroup.getCheckedRadioButtonId()

    Here is my code:

    mRadioButton1=(RadioButton)findViewById(R.id.first);
    mRadioButton2=(RadioButton)findViewById(R.id.second);
    mRadioButton3=(RadioButton)findViewById(R.id.third);
    mRadioButton4=(RadioButton)findViewById(R.id.fourth);
    
    mNextButton=(Button)findViewById(R.id.next_button);
    
    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int selectedId=mRadioGroup.getCheckedRadioButtonId();
    
            int n=0;
            if(selectedId==R.id.first){n=1;}
    
            else if(selectedId==R.id.second){n=2;}
    
            else if(selectedId==R.id.third){n=3;}
    
            else if(selectedId==R.id.fourth){n=4;}
            //. . . .
        }
    

提交回复
热议问题