I'm beginner in Jetpack Compose, so I don't know how can I get value from Radio group that I created.

This is my Radio group composable function:

@Composable
fun KindRadioGroup(
    mItems: List<String>
) {
    val mRememberObserver = remember { mutableStateOf("") }
    CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            mItems.forEach { item ->
                Row(
                    verticalAlignment = Alignment.CenterVertically
                ) {

                    RadioButton(
                        selected = mRememberObserver.value == item,
                        onClick = { mRememberObserver.value = item },
                        enabled = true,
                        colors = RadioButtonDefaults.colors(
                            selectedColor = Color.Magenta
                        )
                    )//RadioButton
                    Text(text = item, modifier = Modifier.padding(start = 8.dp))
                }//Row
            }
        }//Column
    }

}

Is that ok for creating a Radio group or you have better idea?

This is the way that I use that composable function:

val kinds = listOf(stringResource(id = R.string.req_impor), stringResource(id = R.string.nonreq_impor),
    stringResource(id = R.string.req_nonimpor))
KindRadioGroup(mItems = kinds)

By the way if it's ok, how can I get value from selected radio button?

推荐答案

Use state hoisting and move the selection state outside the composable.

Sample code

@Composable
fun KindRadioGroupUsage() {
    val kinds = listOf("Option 1", "Option 2", "Option 3")
    val (selected, setSelected) = remember { mutableStateOf("") }
    Column {
        KindRadioGroup(
            mItems = kinds,
            selected, setSelected
        )
        Text(
            text = "Selected Option : $selected",
            textAlign = TextAlign.Center,
            modifier = Modifier.fillMaxWidth(),
        )
    }
}

@Composable
fun KindRadioGroup(
    mItems: List<String>,
    selected: String,
    setSelected: (selected: String) -> Unit,
) {
    CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            mItems.forEach { item ->
                Row(
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    RadioButton(
                        selected = selected == item,
                        onClick = {
                            setSelected(item)
                        },
                        enabled = true,
                        colors = RadioButtonDefaults.colors(
                            selectedColor = Color.Magenta
                        )
                    )
                    Text(text = item, modifier = Modifier.padding(start = 8.dp))
                }
            }
        }
    }
}

Android相关问答推荐

在android studio中设置pdf文件密码

当 primaryDark 为白色时更改状态栏文本 colored颜色

如何从片段(fragment)中获取工具栏?

在模块 guava-20.0.jar (com.google.guava:guava:20.0) 中发现重复的类 com.google.common.util.concurrent.ListenableFuture

如何从 Sqlite 获取最后一条记录?

为什么 ActionBarActivity 被弃用

将 JSONarray 转换为 ArrayList

如何在Android中将菜单设置为工具栏

以编程方式将 LayoutParams 上的高度设置为与密度无关的像素

如何在活动场景动画过渡期间防止状态栏和导航栏出现动画?

Android:“项目路径必须只有一个段”

Android 5.0 - 向 RecyclerView 添加页眉/页脚

应该有字幕控制器已经设置 Mediaplayer 错误 Android

如何以编程方式圆角并设置随机背景 colored颜色

如何更改TextinputLayout标签的 colored颜色 和edittext下划线android

如何列出android目录中的文件?

Android:TextView自动截断并替换String的最后3个字符

如何在约束布局上实现重叠/负边距?

adb 服务器版本与此客户端不匹配

使用ZXing制作安卓扫码APP