區隔按鈕

使用區隔按鈕,讓使用者從並列的一組選項中選擇。 區隔按鈕分為兩種:

  • 單選按鈕:可讓使用者選擇一個選項。
  • 多選按鈕:可讓使用者選擇 2 到 5 個項目。如需更複雜的選項,或超過五個項目,請使用動態磚
系統會顯示分段按鈕元件。第一個按鈕可單選,第二個按鈕則可複選。
圖 1. 單選按鈕 (1) 和多選按鈕 (2)。

API 介面

使用 SingleChoiceSegmentedButtonRowMultiChoiceSegmentedButtonRow 版面配置建立區隔按鈕。這些版面配置會正確放置及調整 SegmentedButton 的大小,並分享下列重要參數:

  • space:調整按鈕之間的重疊程度。
  • content:包含區隔按鈕列的內容,通常是 SegmentedButton 的序列。

建立單選分段按鈕

以下範例說明如何建立單選區隔按鈕:

@Composable
fun SingleChoiceSegmentedButton(modifier: Modifier = Modifier) {
    var selectedIndex by remember { mutableIntStateOf(0) }
    val options = listOf("Day", "Month", "Week")

    SingleChoiceSegmentedButtonRow {
        options.forEachIndexed { index, label ->
            SegmentedButton(
                shape = SegmentedButtonDefaults.itemShape(
                    index = index,
                    count = options.size
                ),
                onClick = { selectedIndex = index },
                selected = index == selectedIndex,
                label = { Text(label) }
            )
        }
    }
}

程式碼重點

  • 使用 remembermutableIntStateOf 初始化 selectedIndex 變數,追蹤所選按鈕的索引。
  • 定義代表按鈕標籤的 options 清單。
  • SingleChoiceSegmentedButtonRow 只能選取一個按鈕。
  • forEachIndexed 迴圈中,為每個選項建立 SegmentedButton
    • shape 會根據按鈕的索引和使用 SegmentedButtonDefaults.itemShape 的選項總數,定義按鈕的形狀。
    • onClick 會以所點選按鈕的索引更新 onClickselectedIndex
    • selected 會根據 selectedIndex 設定按鈕的選取狀態。
    • label 會使用 Text 可組合函式顯示按鈕標籤。

結果

畫面會顯示區隔按鈕元件,內含「日」、「月」和「週」選項。目前選取的是「天」選項。
圖 2. 已選取一個選項的單選按鈕。

建立多選區隔按鈕

這個範例說明如何建立含有圖示的多選區隔按鈕,讓使用者選取多個選項:

@Composable
fun MultiChoiceSegmentedButton(modifier: Modifier = Modifier) {
    val selectedOptions = remember {
        mutableStateListOf(false, false, false)
    }
    val options = listOf("Walk", "Ride", "Drive")

    MultiChoiceSegmentedButtonRow {
        options.forEachIndexed { index, label ->
            SegmentedButton(
                shape = SegmentedButtonDefaults.itemShape(
                    index = index,
                    count = options.size
                ),
                checked = selectedOptions[index],
                onCheckedChange = {
                    selectedOptions[index] = !selectedOptions[index]
                },
                icon = { SegmentedButtonDefaults.Icon(selectedOptions[index]) },
                label = {
                    when (label) {
                        "Walk" -> Icon(
                            imageVector =
                            Icons.AutoMirrored.Filled.DirectionsWalk,
                            contentDescription = "Directions Walk"
                        )
                        "Ride" -> Icon(
                            imageVector =
                            Icons.Default.DirectionsBus,
                            contentDescription = "Directions Bus"
                        )
                        "Drive" -> Icon(
                            imageVector =
                            Icons.Default.DirectionsCar,
                            contentDescription = "Directions Car"
                        )
                    }
                }
            )
        }
    }
}

程式碼重點

  • 程式碼會使用 remembermutableStateListOf 初始化 selectedOptions 變數。這會追蹤每個按鈕的選取狀態。
  • 程式碼會使用 MultiChoiceSegmentedButtonRow 包含按鈕。
  • forEachIndexed 迴圈中,程式碼會為每個選項建立 SegmentedButton
    • shape:根據按鈕的索引和選項總數定義按鈕形狀。
    • checked 會根據 selectedOptions 中的對應值設定按鈕的勾選狀態。
    • 按一下按鈕時,onCheckedChange 會切換 selectedOptions 中對應項目的選取狀態。
    • icon 會根據 SegmentedButtonDefaults.Icon 和按鈕的勾選狀態顯示圖示。
    • label 會使用適當的圖片向量和內容說明,透過可組合函式顯示與標籤對應的圖示。Icon

結果

畫面顯示分段按鈕元件,內含「步行」、「騎車」和「開車」選項。目前已選取「步行」和「騎乘」選項。
圖 3. 已選取兩個選項的多重選擇區隔按鈕。

其他資源