如何在Android中获取所选单选按钮的ID?

Raa*_*dha 5 android

我正在研究android中的测验应用程序.我们创建了Select.java页面,它显示了sqlite数据库中的问题和选项(带单选按钮).我们还创建了一个header.java文件,用于显示按钮,即Select.java页面的后退和下一个按钮.

在此输入图像描述

在这里,我们需要获取所选的单选按钮ID,并需要将其发送到Header类.因为标题类包含下一个按钮onclick动作.单击下一个按钮后,所选的单选按钮值必须存储在arraylist中.我们在Select.java类中创建了单选按钮.所以我的问题是如何将选定的单选按钮ID转换为下一个按钮单击操作.请帮我解决这个问题.

提前致谢.

KK_*_*585 8

您的布局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>
Run Code Online (Sandbox Code Playgroud)

在您的活动中添加以下ode

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();
            }
        });
Run Code Online (Sandbox Code Playgroud)


Aca*_*tta 5

我知道这是一个古老的问题,但是我在任何地方都看不到我的答案,而且我发现它比其他人更简单

所以我们开始:

int myRadioChecked;
if(radioGroup.getCheckedRadioButtonId() == findViewById(R.id.YOUR_RADIO_BUTTON).getId()) {
  /**Do Stuff*/
  //ex.: myRadioChecked = 1;
}
Run Code Online (Sandbox Code Playgroud)