Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Mit segmentierten Schaltflächen können Nutzer aus einer Reihe von Optionen auswählen, die nebeneinander angezeigt werden.
Es gibt zwei Arten von segmentierten Schaltflächen:
Schaltfläche für die Einzelauswahl: Nutzer können eine Option auswählen.
Schaltfläche für die Mehrfachauswahl: Damit können Nutzer zwischen zwei und fünf Elemente auswählen. Bei komplexeren Auswahlmöglichkeiten oder mehr als fünf Elementen verwenden Sie Chips.
Abbildung 1. Eine Schaltfläche für die Einzelauswahl (1) und eine Schaltfläche für die Mehrfachauswahl (2).
Initialisiert eine selectedIndex-Variable mit remember und mutableIntStateOf, um den Index der ausgewählten Schaltfläche zu verfolgen.
Definiert eine Liste von options, die die Schaltflächenlabels darstellen.
Mit SingleChoiceSegmentedButtonRow können Sie nur eine Schaltfläche auswählen.
Erstellt für jede Option ein SegmentedButton innerhalb der forEachIndexed-Schleife:
shape definiert die Form der Schaltfläche basierend auf ihrem Index und der Gesamtzahl der Optionen mit SegmentedButtonDefaults.itemShape.
onClick wird mit dem Index der angeklickten Schaltfläche aktualisiert.selectedIndex
Mit selected wird der Auswahlstatus der Schaltfläche basierend auf selectedIndex festgelegt.
label zeigt das Schaltflächenlabel mit der zusammensetzbaren Funktion Text an.
Ergebnis
Abbildung 2: Eine Einzelauswahl-Schaltfläche mit einer ausgewählten Option.
Segmentierte Schaltfläche mit Mehrfachauswahl erstellen
In diesem Beispiel wird gezeigt, wie Sie eine segmentierte Schaltfläche mit mehreren Auswahlmöglichkeiten und Symbolen erstellen, mit der Nutzer mehrere Optionen auswählen können:
Alle Inhalte und Codebeispiele auf dieser Seite unterliegen den Lizenzen wie im Abschnitt Inhaltslizenz beschrieben. Java und OpenJDK sind Marken oder eingetragene Marken von Oracle und/oder seinen Tochtergesellschaften.
Zuletzt aktualisiert: 2025-08-27 (UTC).
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Benötigte Informationen nicht gefunden","missingTheInformationINeed","thumb-down"],["Zu umständlich/zu viele Schritte","tooComplicatedTooManySteps","thumb-down"],["Nicht mehr aktuell","outOfDate","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Problem mit Beispielen/Code","samplesCodeIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-27 (UTC)."],[],[],null,["Use a segmented button to let users choose from a set of options, side-by-side.\nThere are two types of segmented buttons:\n\n- **Single-select button**: Lets users choose one option.\n- **Multi-select button** : Lets users choose between two and five items. For more complex choices, or more than five items, use [chips](/develop/ui/compose/components/chip).\n\n**Figure 1.** A single-select button (1) and a multi-select button (2).\n\nAPI surface\n\nUse the [`SingleChoiceSegmentedButtonRow`](/reference/kotlin/androidx/compose/material3/package-summary#SingleChoiceSegmentedButtonRow(androidx.compose.ui.Modifier,androidx.compose.ui.unit.Dp,kotlin.Function1)) and\n[`MultiChoiceSegmentedButtonRow`](/reference/kotlin/androidx/compose/material3/package-summary#MultiChoiceSegmentedButtonRow(androidx.compose.ui.Modifier,androidx.compose.ui.unit.Dp,kotlin.Function1)) layouts to create segmented buttons. These\nlayouts position and size [`SegmentedButton`s](/reference/kotlin/androidx/compose/material3/package-summary#(androidx.compose.material3.MultiChoiceSegmentedButtonRowScope).SegmentedButton(kotlin.Boolean,kotlin.Function1,androidx.compose.ui.graphics.Shape,androidx.compose.ui.Modifier,kotlin.Boolean,androidx.compose.material3.SegmentedButtonColors,androidx.compose.foundation.BorderStroke,androidx.compose.foundation.layout.PaddingValues,androidx.compose.foundation.interaction.MutableInteractionSource,kotlin.Function0,kotlin.Function0)) correctly,\nand share the following key parameters:\n\n- `space`: Adjusts the overlap between the buttons.\n- `content`: Contains the content of the segmented button row, which is typically a sequence of `SegmentedButton`s.\n\nCreate a single-select segmented button\n\nThis example shows how to create a single-select segmented button:\n\n\n```kotlin\n@Composable\nfun SingleChoiceSegmentedButton(modifier: Modifier = Modifier) {\n var selectedIndex by remember { mutableIntStateOf(0) }\n val options = listOf(\"Day\", \"Month\", \"Week\")\n\n SingleChoiceSegmentedButtonRow {\n options.forEachIndexed { index, label -\u003e\n SegmentedButton(\n shape = SegmentedButtonDefaults.itemShape(\n index = index,\n count = options.size\n ),\n onClick = { selectedIndex = index },\n selected = index == selectedIndex,\n label = { Text(label) }\n )\n }\n }\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/components/SegmentedButton.kt#L63-L81\n```\n\n\u003cbr /\u003e\n\nKey points about the code\n\n- Initializes a `selectedIndex` variable using `remember` and `mutableIntStateOf` to track the selected button index.\n- Defines a list of `options` representing the button labels.\n- `SingleChoiceSegmentedButtonRow` lets you select only one button.\n- Creates a `SegmentedButton` for each option, inside the `forEachIndexed` loop:\n - `shape` defines the button's shape based on its index and the total count of options using `SegmentedButtonDefaults.itemShape`.\n - `onClick` updates `selectedIndex` with the clicked button's index.\n - `selected` sets the button's selection state based on `selectedIndex`.\n - `label` displays the button label using the `Text` composable.\n\nResult **Figure 2.** A single-select button with one option picked.\n\nCreate a multi-select segmented button\n\nThis example shows how to create a multi-choice segmented button with icons that\nlets users select multiple options:\n\n\n```kotlin\n@Composable\nfun MultiChoiceSegmentedButton(modifier: Modifier = Modifier) {\n val selectedOptions = remember {\n mutableStateListOf(false, false, false)\n }\n val options = listOf(\"Walk\", \"Ride\", \"Drive\")\n\n MultiChoiceSegmentedButtonRow {\n options.forEachIndexed { index, label -\u003e\n SegmentedButton(\n shape = SegmentedButtonDefaults.itemShape(\n index = index,\n count = options.size\n ),\n checked = selectedOptions[index],\n onCheckedChange = {\n selectedOptions[index] = !selectedOptions[index]\n },\n icon = { SegmentedButtonDefaults.Icon(selectedOptions[index]) },\n label = {\n when (label) {\n \"Walk\" -\u003e Icon(\n imageVector =\n Icons.AutoMirrored.Filled.DirectionsWalk,\n contentDescription = \"Directions Walk\"\n )\n \"Ride\" -\u003e Icon(\n imageVector =\n Icons.Default.DirectionsBus,\n contentDescription = \"Directions Bus\"\n )\n \"Drive\" -\u003e Icon(\n imageVector =\n Icons.Default.DirectionsCar,\n contentDescription = \"Directions Car\"\n )\n }\n }\n )\n }\n }\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/components/SegmentedButton.kt#L91-L132\n```\n\n\u003cbr /\u003e\n\nKey points about the code\n\n- The code initializes the `selectedOptions` variable using `remember` and `mutableStateListOf`. This tracks the selected state of each button.\n- The code uses `MultiChoiceSegmentedButtonRow` to contain the buttons.\n- Inside the `forEachIndexed` loop, the code creates a `SegmentedButton` for each option:\n - `shape` defines the button's shape based on its index and the total count of options.\n - `checked` sets the button's checked state based on the corresponding value in `selectedOptions`.\n - `onCheckedChange` toggles the selected state of the corresponding item in `selectedOptions` when the button is clicked.\n - `icon` displays an icon based on `SegmentedButtonDefaults.Icon` and the button's checked state.\n - `label` displays an icon corresponding to the label, using `Icon` composables with appropriate image vectors and content descriptions.\n\nResult **Figure 3.** A multi-select segmented button with two options picked.\n\nAdditional resources\n\n- Material 3: [Segmented buttons](https://m3.material.io/components/segmented-buttons/overview)"]]