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

后端 未结 10 2153

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条回答
  •  孤城傲影
    2020-12-04 13:35

    My answer is according to the documentation, which works fine.

    1) In xml file include android:onClick="onRadioButtonClicked" as shown below:

    
    
    
    
    

    2) In Java file implement onRadioButtonClicked method outside onCreate() method as shown below:

    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.b1:
                if (checked)
                {
                    Log.e("b1", "b1" );
                    break;
                }
            case R.id.b2:
                if (checked)
                    break;
        }
    }
    

提交回复
热议问题