java - 获取选中的单选按钮

标签 java android

我想创建动态RadioGroup(带有动态单选选项)。我正在使用以下代码进行操作。

我的问题是,如何获取通过单击“检查答案”按钮选择的选项。如果单选按钮组是通过 XML 布局添加的,那么我可以获取它的 ID,但在这种情况下我无法这样做。

代码如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button">
    </LinearLayout>

    <TextView
        android:id="@+id/answer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/button"
        android:textColor="#008b00"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Check Answer"
        app:layout_constraintBottom_toBottomOf="parent"
        android:onClick="checkAnswer"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

主要 Activity :

package com.example.quiz8;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private LinearLayout linearLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
        RadioGroup radioGroup = new RadioGroup(this);
        RadioButton radioButton;

        final String[] title = {"Male", "Female", "Other", "Secret"};

        for (int i = 0; i < title.length; i++) {
            radioButton = new RadioButton(this);
            radioButton.setId(i);
            radioButton.setText(title[i]);
            radioGroup.addView(radioButton);
        }

        linearLayout.addView(radioGroup);
    }

    public void checkAnswer(View view) {
        //get the answer here
        TextView answerText = (TextView) findViewById(R.id.answer);
        answerText.setText("You selected the option...");
    }
}

最佳答案

您可以为 RadioGroup 设置一个标签,以便能够轻松找到它。 标签可以是任何对象。我们将在此处使用 String,它在您调用 findViewWithTag()ViewGroup 中必须是唯一的。然后在 checkAnswer() 中,您通过 tag 检索 RadioGroup,从而获取选中的 RadioButton 的文本。

private static final String RADIOGROUP_TAG = "radiogroup";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
    RadioGroup radioGroup = new RadioGroup(this);
    radioGroup.setTag(RADIOGROUP_TAG);

    RadioButton radioButton;

    final String[] title = {"Male", "Female", "Other", "Secret"};

    for (int i = 0; i < title.length; i++) {
        radioButton = new RadioButton(this);
        radioButton.setId(i);
        radioButton.setText(title[i]);
        radioGroup.addView(radioButton);
    }

    linearLayout.addView(radioGroup);
}

public void checkAnswer(View view) {
    //get the answer here
    RadioGroup radioGroup = linearLayout.findViewWithTag(RADIOGROUP_TAG);
    int checkedId = radioGroup.getCheckedRadioButtonId();
    RadioButton radioButton = radioGroup.findViewById(checkedId);
    String text = radioButton.getText().toString();

    TextView answerText = (TextView) findViewById(R.id.answer);
    answerText.setText("You selected the option..." + text);
}
<小时/>

另一个选项:您可以将 String[] titleRadioGroup 作为 Activity 中的字段。这是有道理的,因为您在多个地方都需要它们。

private final String[] title  = {"Male", "Female", "Other", "Secret"};
private RadioGroup radioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
    radioGroup = new RadioGroup(this);

    RadioButton radioButton;

    for (int i = 0; i < title.length; i++) {
        radioButton = new RadioButton(this);
        radioButton.setId(i);
        radioButton.setText(title[i]);
        radioGroup.addView(radioButton);
    }

    linearLayout.addView(radioGroup);
}

public void checkAnswer(View view) {
    //get the answer here
    int checkedId = radioGroup.getCheckedRadioButtonId();
    TextView answerText = (TextView) findViewById(R.id.answer);
    // Note: make sure there is a checked RadioButton! 
    answerText.setText("You selected the option..." + title[checkedId]);
}

关于java - 获取选中的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59039195/

相关文章:

java - 帮助我使用 Stateful beans

android - 如何使用 Sybase-iAnywhere-Blue-SDK-for-Android 在 android 中进行蓝牙打印?

java - java String 和 String Builder 问题

java - 避免重复记录 - HashSet

java - 从 URL 中提取随机生成的 ID

java - openssl smime - 通过 Java 解密 PEM 编码的文件

Android 应用内采购订单 ID GPA 123456789

java - "getName"的 GetMethodID 返回 NULL

android - 有没有一种方法可以设置和合并而无需仅为 1 个值创建映射

java - 将空值分配给有值(value)的值或在 Java 中不分配任何值之间有什么区别?