You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose中检测RadioButton选中项并实现问卷单选及结果存储

问卷功能实现方案

核心思路

通过键值对状态存储每个问题的选中答案,天然保证每个问题仅对应一个选项,同时可以直接转换为要求的结果格式。


1. 单选限制实现

你只需要创建一个可观察的映射状态,以问题为键、选中的答案为值,即可保证每个问题最多只有一个选中项:

// 存储每个问题的选中答案,key为问题内容,value为选中的选项
val selectedAnswers = remember { mutableStateMapOf<String, String>() }

渲染每个问题的选项时,RadioButton的选中状态绑定该映射的值,点击时更新对应问题的选中值即可,同个问题的其他选项会自动失选,无需额外处理互斥逻辑。


2. 问卷结果导出

直接遍历原始问题列表,从状态映射中取出对应答案,拼接为要求的字符串格式即可:

// 生成要求格式的结果列表,未作答的问题会自动过滤
val surveyResult = surveyQuestions.mapNotNull { question ->
    selectedAnswers[question.content]?.let { answer ->
        "${question.content} - $answer"
    }
}

如果需要保留未作答的问题,把mapNotNull换成map,给未作答的返回默认占位字符串即可。


完整可运行示例代码

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

// 你的Question数据类定义
data class Question(val content: String, val options: List<String>)

val surveyQuestions = listOf(
    Question("question 1", listOf("answer 1", "answer 2")),
    Question("question 2", listOf("answer 1", "answer 2", "answer 3")),
    Question("question 3", listOf("answer 1", "answer 2"))
)

@Composable
fun SurveyScreen() {
    val selectedAnswers = remember { mutableStateMapOf<String, String>() }
    
    Column(modifier = Modifier.padding(16.dp)) {
        LazyColumn(modifier = Modifier.weight(1f)) {
            // 遍历所有问题
            items(surveyQuestions) { question ->
                Text(text = question.content, modifier = Modifier.padding(vertical = 8.dp))
                // 遍历当前问题的所有选项
                question.options.forEach { option ->
                    Row {
                        RadioButton(
                            selected = selectedAnswers[question.content] == option,
                            onClick = {
                                // 更新当前问题的选中答案
                                selectedAnswers[question.content] = option
                            }
                        )
                        Text(text = option, modifier = Modifier.padding(start = 8.dp))
                    }
                }
            }
        }
        
        // 提交按钮,点击可获取结果
        Button(onClick = {
            val surveyResult = surveyQuestions.mapNotNull { question ->
                selectedAnswers[question.content]?.let { answer ->
                    "${question.content} - $answer"
                }
            }
            // 此处可处理surveyResult,比如打印、上传等
            println(surveyResult)
        }) {
            Text("提交问卷")
        }
    }
}

界面预览

问卷视图预览


内容的提问来源于stack exchange,提问作者Wafi_ck

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 02:57:00