簡體   English   中英

如何在android中獲取所選單選按鈕的ID?

[英]How to get the id of selected radio button in android?

我正在 android 中開發測驗應用程序。 我們已經創建了Select.java頁面,它顯示了來自 sqlite 數據庫的問題和選項(帶有單選按鈕)。 我們還創建了一個header.java文件來顯示按鈕,即 Select.java 頁面的后退和下一個按鈕。

在此處輸入圖片說明

在這里,我們需要獲取選定的單選按鈕 id 並將其發送到 Header 類。 因為標題類包含下一個按鈕 onclick 操作。 單擊下一個按鈕后,選定的單選按鈕值必須存儲在數組列表中。 我們在 Select.java 類中創建了單選按鈕。 所以我的問題是如何將選定的單選按鈕 id 放入下一個按鈕單擊操作中。 請幫我解決這個問題。

提前致謝。

你的布局xml文件應該是這樣的

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <RadioGroup 
    android:orientation="vertical"
    android:id="@+id/radiogroup"
     android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    >
    <RadioButton 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/option1"
       android:text="Option1"
    />
    <RadioButton 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/option2"
       android:text="Option2"
    />
    <RadioButton 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/option3"
       android:text="Option3"
    />
    <RadioButton 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/option4"
       android:text="Option4"
    />
    <RadioButton 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/option5"
       android:text="Option5"
    />
    </RadioGroup>
</LinearLayout>

在您的活動中添加以下頌歌

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {
                RadioButton checkedRadioButton = (RadioButton) findViewById(checkedId);
                String text = checkedRadioButton.getText().toString();
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }
        });

我知道這是一個老問題,但我在任何地方都沒有看到我的答案,而且我發現它比其他人更簡單

所以我們開始:

int myRadioChecked;
if(radioGroup.getCheckedRadioButtonId() == findViewById(R.id.YOUR_RADIO_BUTTON).getId()) {
  /**Do Stuff*/
  //ex.: myRadioChecked = 1;
}
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.MyRadioGroup);

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1) {
        int selectedId = radioGroup.getCheckedRadioButtonId();
        Log.i("ID", String.valueOf(selectedId));

    }
});

您可以通過以下獲得所選按鈕的id 這個。這里

int position = group.indexOfChild(radioButton);

會給你 id.Also 你也可以 Toast 看到這樣的 id

 Toast.makeText(MainActivity.this,"Id of radio button"+position+, Toast.LENGTH_SHORT).show();

如果您單擊第一個按鈕,則會彈出 - “您單擊的單選按鈕的 ID 為 0”。

在 RadioGroup 類中,您可以使用方法getCheckedRadioButtonId();

RadioGroup rg = findViewById(R.id.radioGroup); rg.getCheckedRadioButtonId();

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

嗯,只需在 UserBO 中再添加一個成員變量來存儲選定的答案。

Class UserBO {

private int userID;
private String userName;
private String question;
private String option1;
private String option2;
private String option3;

private int answerID;

//create getter and setters for above member variables
} 

然后在 Adapter 類的 onclick 偵聽器中,執行如下操作

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup radioGroup,
                int radioButtonID) {
        switch(radioButtonID) {
                case R.id.option1:
                       listItem.setAnswerID(1);
            break;
        case R.id.option2:
                listItem.setAnswerID(2);
            break;

                 }
        }
    });

然后更改您的標頭構造函數以接收 userarraylist (其中包含用戶詳細信息和答案)

ArrayList<USerBO> userList;
Header(Context context, AttributeSet attrs, ArrayList<UserBO> userALt) {
userList = userAL;
}

//on next button click

onclick() {
    for(UserBO userObj: userList) {
        if (userObj.getAnswerID != 0)
         Log.d("AnswerID", userObj.getAnswerID);
    }
}

它就像 sudo 代碼.. 我希望這會幫助你..

這是最好的方法:

RadioButton button = findViewById(v.getId());

暫無
暫無

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

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