圖示按鈕會顯示使用者可執行的動作。圖示按鈕必須使用含義明確的圖示,通常代表常見或常用的動作。
圖示按鈕分為兩種類型:
- 預設:這些按鈕可開啟其他元素,例如選單或搜尋。
- 切換鈕:這些按鈕可代表能切換開啟或關閉的二進位動作,例如「收藏」或「書籤」。
API 介面
使用 IconButton 可組合項實作標準圖示按鈕。如要建立不同的視覺樣式 (例如填滿、填滿色調或外框),請分別使用 FilledIconButton、FilledTonalIconButton 和 OutlinedIconButton。
IconButton 的重要參數包括:
onClick:使用者輕觸圖示按鈕時執行的 lambda 函式。enabled:控制按鈕啟用狀態的布林值。當false時,按鈕不會回應使用者輸入內容。content:按鈕內的可組合內容,通常是Icon。
基本範例:切換圖示按鈕
這個範例說明如何實作切換圖示按鈕。切換圖示按鈕的外觀會根據是否選取而改變。
@Preview @Composable fun ToggleIconButtonExample() { // isToggled initial value should be read from a view model or persistent storage. var isToggled by rememberSaveable { mutableStateOf(false) } IconButton( onClick = { isToggled = !isToggled } ) { Icon( painter = if (isToggled) painterResource(R.drawable.favorite_filled) else painterResource(R.drawable.favorite), contentDescription = if (isToggled) "Selected icon button" else "Unselected icon button." ) } }
程式碼重點
ToggleIconButtonExample可組合項會定義可切換的IconButton。mutableStateOf(false)會建立一個MutableState物件,其中包含布林值,初始值為false。這會使isToggled成為狀態容器,也就是說,每當值變更時,Compose 都會重組 UI。rememberSaveable可確保isToggled狀態在設定變更 (例如螢幕旋轉) 時仍能保留。
IconButton的onClicklambda 會定義按鈕點擊時的行為,在true和false之間切換狀態。Icon可組合項的painter參數會根據isToggled狀態,有條件地載入不同的painterResource。這會變更圖示的外觀。- 如果
isToggled為true,則會載入填滿的愛心可繪項目。 - 如果
isToggled為false,則會載入愛心外框可繪項目。
- 如果
Icon的contentDescription也會根據isToggled狀態更新,提供適當的無障礙資訊。
結果
下圖顯示上述程式碼片段中,切換圖示按鈕未選取的狀態:

進階範例:按下時重複執行動作
本節說明如何建立圖示按鈕,讓使用者按住按鈕時持續觸發動作,而非每次點擊只觸發一次。
@Composable fun MomentaryIconButton( unselectedImage: Int, selectedImage: Int, contentDescription: String, modifier: Modifier = Modifier, stepDelay: Long = 100L, // Minimum value is 1L milliseconds. onClick: () -> Unit ) { val interactionSource = remember { MutableInteractionSource() } val isPressed by interactionSource.collectIsPressedAsState() val pressedListener by rememberUpdatedState(onClick) LaunchedEffect(isPressed) { while (isPressed) { delay(stepDelay.coerceIn(1L, Long.MAX_VALUE)) pressedListener() } } IconButton( modifier = modifier, onClick = onClick, interactionSource = interactionSource ) { Icon( painter = if (isPressed) painterResource(id = selectedImage) else painterResource(id = unselectedImage), contentDescription = contentDescription, ) } }
程式碼重點
MomentaryIconButton採用unselectedImage: Int(未按下按鈕時圖示的可繪製資源 ID) 和selectedImage: Int(按下按鈕時圖示的可繪製資源 ID)。- 它會使用
interactionSource,專門追蹤使用者的「按下」互動。 - 當按鈕處於按下狀態時,
isPressed為 true,否則為 false。當isPressed為true時,LaunchedEffect會進入迴圈。- 在這個迴圈中,系統會使用
delay(搭配stepDelay) 在觸發動作之間建立暫停。coerceIn可確保延遲時間至少為 1 毫秒,防止無限迴圈。 - 每次迴圈延遲後,系統都會叫用
pressedListener。這會讓動作重複。
- 在這個迴圈中,系統會使用
pressedListener會使用rememberUpdatedState,確保onClicklambda (要執行的動作) 一律是最新組合中的最新版本。Icon會根據按鈕目前是否按下,變更顯示的圖片。- 如果
isPressed為 true,則會顯示selectedImage。 - 否則會顯示
unselectedImage。
- 如果
接下來,請在範例中使用這個 MomentaryIconButton。下列程式碼片段示範兩個控制計數器的圖示按鈕:
@Preview() @Composable fun MomentaryIconButtonExample() { var pressedCount by remember { mutableIntStateOf(0) } Row( modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically ) { MomentaryIconButton( unselectedImage = R.drawable.fast_rewind, selectedImage = R.drawable.fast_rewind_filled, stepDelay = 100L, onClick = { pressedCount -= 1 }, contentDescription = "Decrease count button" ) Spacer(modifier = Modifier) Text("advanced by $pressedCount frames") Spacer(modifier = Modifier) MomentaryIconButton( unselectedImage = R.drawable.fast_forward, selectedImage = R.drawable.fast_forward_filled, contentDescription = "Increase count button", stepDelay = 100L, onClick = { pressedCount += 1 } ) } }
程式碼重點
MomentaryIconButtonExample可組合函式會顯示包含兩個MomentaryIconButton執行個體的Row,以及Text可組合函式,用於建構 UI 來增加及減少計數器。- 它會使用
remember和mutableIntStateOf維護pressedCount可變動狀態變數,並初始化為 0。當pressedCount變更時,任何觀察該值的可組合函式 (例如Text可組合函式) 都會重組,以反映新值。 - 點選或按住第一個
MomentaryIconButton,可減少pressedCount。 - 第二個
MomentaryIconButton則會在點選或按住時增加pressedCount。 - 兩個按鈕都使用 100 毫秒的
stepDelay,這表示按住按鈕時,onClick動作每 100 毫秒重複一次。
結果
以下影片顯示含有圖示按鈕和計數器的 UI: