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

后端 未结 10 2135

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:

    <RadioGroup
    android:id="@+id/rg1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">
    <RadioButton
        android:id="@+id/b1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="onCreateOptionsMenu()"
        android:onClick="onRadioButtonClicked"
                />
    <RadioButton
        android:id="@+id/b2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="onCreateMenu()"
        android:onClick="onRadioButtonClicked"
                />
    </RadioGroup>
    

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-04 13:38

    I used the following and it worked for me. I used a boolean validation function where if any Radio Button in a Radio Group is checked, the validation function returns true and a submission is made. if returned false, no submission is made and a toast to "Select Gender" is shown:

    1. MainActivity.java

      public class MainActivity extends AppCompatActivity {
          private RadioGroup genderRadioGroup;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          //Initialize Radio Group And Submit Button
          genderRadioGroup=findViewById(R.id.gender_radiogroup);
          AppCompatButton submit = findViewById(R.id.btnSubmit);
      
          //Submit Radio Group using Submit Button
          submit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  //Check Gender radio Group For Selected Radio Button
                  if(genderRadioGroup.getCheckedRadioButtonId()==-1){//No Radio Button Is Checked
                      Toast.makeText(getApplicationContext(), "Please Select Gender", Toast.LENGTH_LONG).show();
                  }else{//Radio Button Is Checked
                      RadioButton selectedRadioButton = findViewById(genderRadioGroup.getCheckedRadioButtonId());
                      gender = selectedRadioButton == null ? "" : selectedRadioButton.getText().toString().trim();
                  }
                  //Validate
                  if (validateInputs()) {
                      //code to proceed when Radio button is checked
                  }
              }
          }
          //Validation - No process is initialized if no Radio button is checked
          private boolean validateInputs() {
              if (genderRadioGroup.getCheckedRadioButtonId()==-1) {
                  return false;
              }
              return true;
          }
      }
      
    2. In activity_main.xml:

      <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
      
          <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/gender"/>
      
          <RadioGroup
                  android:id="@+id/gender_radiogroup"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
      
              <RadioButton
                      android:id="@+id/male"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/male"/>
      
              <RadioButton
                      android:id="@+id/female"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/female""/>
      
          </RadioGroup>
      
          <android.support.v7.widget.AppCompatButton
              android:id="@+id/btnSubmit"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@string/submit"/>
      
      </LinearLayout>
      

    If no gender radio button is selected, then the code to proceed will not run.

    I hope this helps.

    0 讨论(0)
  • 2020-12-04 13:39
      rgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch(i) {
                    case R.id.type_car:
                        num=1;
                        Toast.makeText(getApplicationContext()," Car",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.type_bike:
                        num=2;
                        Toast.makeText(getApplicationContext()," Bike",Toast.LENGTH_LONG).show();
                        break;
                }
            }
        });
    
    0 讨论(0)
  • 2020-12-04 13:39

    try to use this

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:orientation="horizontal"
    >    
        <RadioButton
            android:id="@+id/standard_delivery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Standard_delivery"
            android:checked="true"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="15dp"
            android:textSize="12dp"
            android:onClick="onRadioButtonClicked"   
        />
    
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Midnight_delivery"
            android:checked="false"
            android:layout_marginRight="15dp"
            android:layout_marginTop="4dp"
            android:textSize="12dp"
            android:onClick="onRadioButtonClicked"
            android:id="@+id/midnight_delivery"
        />    
    </RadioGroup>
    

    this is java class

    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.standard_delivery:
                    if (checked)
                        Toast.makeText(DishActivity.this," standard delivery",Toast.LENGTH_LONG).show();
                        break;
                case R.id.midnight_delivery:
                    if (checked)
                        Toast.makeText(DishActivity.this," midnight delivery",Toast.LENGTH_LONG).show();
                        break;
            }
        }
    
    0 讨论(0)
提交回复
热议问题