在没有单选按钮的情况下在 Jetpack Compose 中创建切换按钮组
Create Toggle Button Group in Jetpack Compose without Radio Buttons
我试图在我的项目中放置一个按钮切换组,其行为类似于单选按钮组,但看起来不像单选按钮组(即当一个按钮被选中时,其他按钮被取消选择)。
我按照我在网上找到的单选按钮模式进行操作,但这似乎并不奏效。有没有办法做到这一点?我已经到了我想要的地方有按钮的地步,但它们都被禁用了。
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,
),
)
}
}
}
}
我试图在我的项目中放置一个按钮切换组,其行为类似于单选按钮组,但看起来不像单选按钮组(即当一个按钮被选中时,其他按钮被取消选择)。
我按照我在网上找到的单选按钮模式进行操作,但这似乎并不奏效。有没有办法做到这一点?我已经到了我想要的地方有按钮的地步,但它们都被禁用了。
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,
),
)
}
}
}
}