如何在Android中检查radiobutton是否在radiogroup中被选中?

如何在Android中检查radiobutton是否在radiogroup中被选中?,android,radio-button,radio-group,Android,Radio Button,Radio Group,我需要设置验证,用户必须填写/选择页面中的所有详细信息。如果任何字段为空,则希望显示要填充的Toast消息。现在我需要为RadioGroup中的RadioButton设置验证。我尝试了此代码,但没有正常工作。建议我正确的方法。谢谢 // get selected radio button from radioGroup int selectedId = gender.getCheckedRadioButtonId(); // find the radiobutton by returned id

我需要设置验证,用户必须填写/选择页面中的所有详细信息。如果任何字段为空,则希望显示要填充的
Toast消息。
现在我需要为
RadioGroup中的
RadioButton
设置验证。
我尝试了此代码,但没有正常工作。建议我正确的方法。谢谢

// get selected radio button from radioGroup
int selectedId = gender.getCheckedRadioButtonId();
// find the radiobutton by returned id
selectedRadioButton = (RadioButton)findViewById(selectedId);
// do what you want with radioButtonText (save it to database in your case)
radioButtonText = selectedRadioButton.getText().toString();

if(radioButtonText.matches(""))
{
    Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
    Log.d("QAOD", "Gender is Null");
}
else
{
    Log.d("QAOD", "Gender is Selected");
}
对每个必须检查的单选按钮使用isChecked()函数

RadioButton maleRadioButton, femaleRadioButton;

maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButton);
femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButton);
然后将结果用于您的if/else案例考虑

if (maleRadioButton.isChecked() || femaleRadioButton.isChecked()) {
     Log.d("QAOD", "Gender is Selected");
} else {
    Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
    Log.d("QAOD", "Gender is Null");
}

尝试使用isChecked()方法

selectedRadioButton.isChecked()->返回布尔值


有关单选按钮的更多详细信息,请参阅。如果只想查看一个单选按钮,可以使用
isChecked
功能

if(radioButton.isChecked())
{
  // is checked    
}
else
{
  // not checked
}
如果您有一个
RadioGroup
,您可以使用

if (radioGroup.getCheckedRadioButtonId() == -1)
{
  // no radio buttons are checked
}
else
{
  // one of the radio buttons is checked
}

您只需使用
getCheckedRadioButtonId()
isChecked()
方法

if(gender.getCheckedRadioButtonId()==-1)
{
    Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
}
else
{
    // get selected radio button from radioGroup
    int selectedId = gender.getCheckedRadioButtonId();
    // find the radiobutton by returned id
    selectedRadioButton = (RadioButton)findViewById(selectedId);
    Toast.makeText(getApplicationContext(), selectedRadioButton.getText().toString()+" is selected", Toast.LENGTH_SHORT).show();
}

我使用单选按钮的id与我使用
mRadioGroup.getCheckedRadioButtonId()获得的
checkedRadioButton
的id进行比较

这是我的密码:

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;}
        //. . . .
    }
试着用这个

<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>
我的答案是根据,这很好

1) 在xml文件中包括android:onClick=“onRadioButtonClicked”
,如下所示:

<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>
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;
    }
}

我使用了下面的方法,它对我有效。我使用了一个布尔验证函数,如果选中了单选组中的任何单选按钮,验证函数将返回true并进行提交。如果返回false,则不提交,并显示为“选择性别”干杯:

  • 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;
        }
    }
    
  • 在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>
    


    如果(radioGroup.getCheckedRadioButtonId()==radioGroup.getChildAt(radio1.getId()){radio1已选中;}否则如果(…==radioGroup.getChildAt(radio2)){radio2已选中;}。。。使用此选项,可以获得选定的单选值。:)您好@RipalTamboli谢谢您的评论,但我可以得到单选按钮的值和文本。我需要验证用户是否未检查性别(单选组),单击按钮时将显示错误消息。确定。如果没有选择任何选项,那么getCheckedRadioButtonID()返回什么?如果它返回null/-1,则通过检查它是否为null/-1来完成工作。:)性别组中的nwys,默认情况下始终需要选择一个选项。这是设计它的标准方式:)。。如果有任何疑问,请告诉我。谢谢您的回答。我需要检查整个广播组,而不是单个单选按钮。你去吧。对您的RadioGroup使用getCheckedRadioButtonId()方法来查找。当组中没有选择单选按钮时,返回-1。你已经在这样做了。因此,如果selectedId为-1,则表示未选择性别。否则,组中会选择一些内容。嗨,谢谢你的回答,我需要检查整个广播组而不是单个单选按钮。你能添加一些关于这项功能的评论以及它解决问题的原因吗?我已经在上面的帖子中添加了评论。我希望这些评论能说明代码的作用以及它解决问题的原因。非常有用。谢谢你的回答。
    <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>
    
      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;
                }
            }
        });
    
    mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (mRadioButtonMale.isChecked()) {
                    text = "male";
                } else {
                    text = "female";
                }
            }
        });
    
     mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (mRadioButtonMale.isChecked()) { text = "male"; }
                if(mRadioButtonFemale.isChecked()) { text = "female"; }
            }
        });
    
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                    checkedId = radioGroup.getCheckedRadioButtonId();
    
                    switch (checkedId) {
                        case R.id.expert_rb:
                            // do something
                            break;
                        case R.id.no_expert_rb:
                            // do something else
                            break;
                       
                    }
                }
            });