gpt4 book ai didi

android - 在没有单选按钮的 Jetpack Compose 中创建切换按钮组

转载 作者:行者123 更新时间:2023-12-04 23:54:50 36 4
gpt4 key购买 nike

我试图在我的项目中放置一个按钮切换组,其行为类似于单选按钮组,但看起来不像单选按钮组(即,当一个按钮被选中时,其他按钮被取消选择)。
我遵循了我在网上找到的单选按钮模式,但这似乎没有奏效。有没有办法做到这一点?我已经到了在我想要的地方有按钮的地步,但它们都被禁用了。

MovieSpotterTheme() {
Card(
modifier = Modifier
.fillMaxWidth()
) {
@Composable
fun MaterialButtonToggleGroup() {
var selected by remember { mutableStateOf("Android") }

val buttonGroup = listOf("Popular Movies", "Search Movies")


val onSelectedChange = { text: String ->
selected = text
}
Row(
horizontalArrangement = Arrangement.SpaceEvenly
) {
buttonGroup.forEach { text ->
Row(Modifier
.selectable(
selected = (text == selected),
onClick = { onSelectedChange(text) }
)
.padding(horizontal = 16.dp)
) {
Button(
enabled = (text == selected),
onClick = { onSelectedChange(text) }
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = text,
style = MaterialTheme.typography.body1.merge(),
modifier = Modifier.padding(horizontal = 16.dp)
)
}
}
}
}
}
}
Surface() {
MaterialButtonToggleGroup()
}
}
}

最佳答案

提供简化版本。玩弄它以满足您的要求。

@Composable
fun CustomRadioGroup() {
val options = listOf(
"Option 1",
"Option 2",
"Option 3",
"Option 4",
)
var selectedOption by remember {
mutableStateOf("")
}
val onSelectionChange = { text: String ->
selectedOption = text
}

Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize(),
) {
options.forEach { text ->
Row(
modifier = Modifier
.padding(
all = 8.dp,
),
) {
Text(
text = text,
style = typography.body1.merge(),
color = Color.White,
modifier = Modifier
.clip(
shape = RoundedCornerShape(
size = 12.dp,
),
)
.clickable {
onSelectionChange(text)
}
.background(
if (text == selectedOption) {
Color.Magenta
} else {
Color.LightGray
}
)
.padding(
vertical = 12.dp,
horizontal = 16.dp,
),
)
}
}
}
}
Radio Group

关于android - 在没有单选按钮的 Jetpack Compose 中创建切换按钮组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69788366/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com