簡體   English   中英

從android中的每個radiogroup中選擇至少一個單選按鈕?

[英]Checking at least one radio button is selected from each radiogroup in android?

如何確保用戶從每個放射組中至少檢查一個radiobutton。 就像我有這個無線電組:

<RadioGroup
    android:id="@+id/myradio_group_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first"
        android:checked="true" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="third" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fourt" />
</RadioGroup>

<RadioGroup
    android:id="@+id/myradio_group_two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first"
        android:checked="true" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="third" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fourt" />
</RadioGroup>

我想以編程方式檢查用戶是否選擇了至少一個radiobutton?

獲取RadioGroup的引用,然后在無線電組上調用getCheckedRadioButtonId()如果未選擇任何內容,則調用將返回-1

請參閱public int getCheckedRadioButtonId ()

RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits); 

// Returns an integer which represents the selected radio button's ID
int selected = g.getCheckedRadioButtonId();

// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);

// Now you can get the text or whatever you want from the "selected" radio button
b.getText();
if (radioGroup.getCheckedRadioButtonId() != -1)
{
  // hurray at-least on radio button is checked. 
}
else
{
  // pls select at-least one radio button.. since id is -1 means no button is check
}

這是我在我的一個測驗應用程序中使用的代碼..看看代碼,如果這有幫助..

private boolean checkAnswer(){
        String answer = getSelection();
        if(answer==null) {
            Log.d("Questions", "No checkbox selected");
            return false;
        }
        else {
        //  Do your stuff here
            return true;
        }

    }


    private String getSelection(){


        if (c1.isChecked())
        {
            return c1.getText().toString();
        }
        if (c2.isChecked())
        {
            return c2.getText().toString();
        }
        if (c3.isChecked())
        {
            return c3.getText().toString();
        }
        if (c4.isChecked())
        {
            return c4.getText().toString();
        }

        return null;

    }

您可以使用此方法

radioGroup.getCheckedRadioButtonId();

它返回該組中所選單選按鈕的標識符。 選擇空時,返回值為-1。

如文檔中所述https://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM