如何获得选定的单选按钮的ID在Android的?

问题描述:

我的工作测验应用程序的Andr​​oid。我们从SQLite数据库中创建的 Select.java 页其中显示问题和选项(开关按钮)。此外,我们创建了一个 header.java 文件显示按钮,即背和下一个按钮为Select.java页面。

I am working on quiz application in android. We have created Select.java page which displays the questions and options(with radio buttons) from sqlite database. Also we created a header.java file for displaying buttons i.e back and next buttons for the Select.java page.

在这里,我们需要得到选定的单选按钮的ID和需要向其发送至头类。由于头类由下一个按钮的onclick行动。一旦下一个按钮被点击选中的单选按钮的值必须被存储在数组列表。我们在Select.java类中创建单选按钮。所以我的问题是如何获得所选择的单选按钮标识成下一个按钮点击动作。请帮我对此。

Here we need to get the selected radio button id and need to send that to Header class. Because header class consists of the next button onclick action. Once the next button is clicked the selected radio button value has to be stored in arraylist. We created radio buttons in Select.java class. So my question is how to get the selected radio button id into that next button click action. Please help me regarding this.

在此先感谢。

您的布局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();
            }
        });