實體按鈕
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
穿戴式裝置通常含有多個實體按鈕 (也稱為 Stem)。Wear OS 裝置至少須有一個按鈕,亦即電源按鈕。除此之外,也可能會有其他的多功能按鈕。
在應用程式中,您可以將多功能按鈕指派給動作。舉例來說,健身應用程式可能會使用多功能按鈕開始或暫停健身:
注意:Wear OS 3.0 會保留兩個按鈕供作業系統使用,而 Wear OS 2.0 則只會保留一個按鈕。這樣會減少可以指派動作的按鈕數量。
本指南說明如何擷取裝置上多功能按鈕的相關資訊,以及如何處理按鈕的按下動作。
如要取得裝置上按鈕的額外資訊,請使用 Wear Input AndroidX 程式庫中定義的 API。在應用程式模組的 build.gradle
檔案中新增下列依附元件。
dependencies {
implementation "androidx.wear:wear-input:1.0.0"
}
如要查看裝置可使用的按鈕數量,請使用 WearableButtons.getButtonCount()
方法。此方法傳回的按鈕數量包含電源按鈕,因此若傳回的值大於一,表示有多功能按鈕可供使用。如要準確計算可指派的多功能按鈕數量,請將按鈕總數扣除一,因為第一個按鈕一律是電源按鈕。
每個按鈕都對應至 KeyEvent
類別中的 int
常數,如下表所示:
按鈕
|
KeyEvent
|
多功能按鈕 1 |
KEYCODE_STEM_1
|
多功能按鈕 2 |
KEYCODE_STEM_2
|
多功能按鈕 3 |
KEYCODE_STEM_3
|
下列程式碼範例說明如何取得可用的按鈕數量資訊:
Kotlin
val count = WearableButtons.getButtonCount(context)
if (count > 1) {
// There are multifunction buttons available
}
val buttonInfo = WearableButtons.getButtonInfo(activity, KeyEvent.KEYCODE_STEM_1)
if (buttonInfo == null) {
// KEYCODE_STEM_1 is unavailable
} else {
// KEYCODE_STEM_1 is present on the device
}
Java
int count = WearableButtons.getButtonCount(context);
if (count > 1) {
// There are multifunction buttons available
}
WearableButtons.ButtonInfo buttonInfo =
WearableButtons.getButtonInfo(activity, KeyEvent.KEYCODE_STEM_1);
if (buttonInfo == null) {
// KEYCODE_STEM_1 is unavailable
} else {
// KEYCODE_STEM_1 is present on the device
}
應用程式有多種可能的按鈕按鍵碼:
-
KEYCODE_STEM_1
-
KEYCODE_STEM_2
-
KEYCODE_STEM_3
您的應用程式可接收這些按鈕按鍵碼,並轉換為應用程式內特定動作。
如要處理按下按鈕動作,請實作 onKeyDown()
方法。
舉例來說,此實作會回應某個按下按鈕的動作,以控制應用程式中的動作:
Kotlin
// Activity
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
return if (event.repeatCount == 0) {
when (keyCode) {
KeyEvent.KEYCODE_STEM_1 -> {
// Do stuff
true
}
KeyEvent.KEYCODE_STEM_2 -> {
// Do stuff
true
}
KeyEvent.KEYCODE_STEM_3 -> {
// Do stuff
true
}
else -> {
super.onKeyDown(keyCode, event)
}
}
} else {
super.onKeyDown(keyCode, event)
}
}
Java
@Override
// Activity
public boolean onKeyDown(int keyCode, KeyEvent event){
if (event.getRepeatCount() == 0) {
if (keyCode == KeyEvent.KEYCODE_STEM_1) {
// Do stuff
return true;
} else if (keyCode == KeyEvent.KEYCODE_STEM_2) {
// Do stuff
return true;
} else if (keyCode == KeyEvent.KEYCODE_STEM_3) {
// Do stuff
return true;
}
}
return super.onKeyDown(keyCode, event);
}
決定按鈕的位置
AndroidX 程式庫提供兩種說明按鈕位置的方法:
注意:描述按鈕及功能時,建議不要使用文字描述元,改用視覺化指標。但在某些情況下,則適合使用文字說明。
上述方法是專為簡要說明而設計。如果這些 API 不符合您應用程式的需求,您也可以使用 WearableButtons.getButtonInfo()
API 取得按鈕在畫面中的位置,並且透過自己希望的方式處理。如要進一步瞭解 API,請參閱「Wear API 參考資料」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],null,["# Physical buttons\n\nA wearable device typically contains multiple physical buttons, also known as _stems_. Wear OS\ndevices always have, at minimum, one button: the power button. Beyond that, zero or more\nmultifunction buttons might be present.\n\n\nIn your app, you can assign multifunction buttons to actions. For example, a fitness app\nmight start or pause a workout using multifunction buttons:\n\n**Note:** Wear OS 3.0 reserves two buttons for the OS, while\nWear OS 2.0 reserves only one. This reduces the number of buttons you can assign\nactions to. \n\nFor suitable use cases and design considerations, review the\n[Wear OS design principles](/training/wearables/design).\n\n\nThis guide describes how to retrieve information about available multifunction buttons on\na device and how to process button presses.\n\nButton metadata\n---------------\n\n\nTo get extra information about the buttons on a device, use the API defined in the\n[Wear Input](/reference/androidx/wear/input/package-summary) AndroidX library. Add the\nfollowing dependency in your app module's `build.gradle` file: \n\n```groovy\ndependencies {\nimplementation \"androidx.wear:wear-input:1.0.0\"\n}\n```\n\n### Number of buttons\n\n\nTo find out how many buttons are available on the device, use the\n[`WearableButtons.getButtonCount()`](/reference/android/support/wearable/input/WearableButtons#getButtonCount(android.content.Context)) method. This method includes the power button,\nso if the method returns a value greater than one, then there are multifunction buttons available\nfor use. To get an accurate count of assignable multifunction buttons, subtract\none from the count, since the first button is always the power button.\n\n### Keycodes for button presses\n\n\nEach button is mapped to an `int` constant from the [KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html)\nclass, as shown in the following table:\n\n| Button | KeyEvent |\n|------------------------|------------------|\n| Multifunction button 1 | `KEYCODE_STEM_1` |\n| Multifunction button 2 | `KEYCODE_STEM_2` |\n| Multifunction button 3 | `KEYCODE_STEM_3` |\n\n\nThe following example code shows how to get the available button\ncount: \n\n### Kotlin\n\n```kotlin\nval count = WearableButtons.getButtonCount(context)\n\nif (count \u003e 1) {\n // There are multifunction buttons available\n}\n\nval buttonInfo = WearableButtons.getButtonInfo(activity, KeyEvent.KEYCODE_STEM_1)\n\nif (buttonInfo == null) {\n // KEYCODE_STEM_1 is unavailable\n} else {\n // KEYCODE_STEM_1 is present on the device\n}\n```\n\n### Java\n\n```java\nint count = WearableButtons.getButtonCount(context);\n\nif (count \u003e 1) {\n // There are multifunction buttons available\n}\n\nWearableButtons.ButtonInfo buttonInfo =\n WearableButtons.getButtonInfo(activity, KeyEvent.KEYCODE_STEM_1);\n\nif (buttonInfo == null) {\n // KEYCODE_STEM_1 is unavailable\n} else {\n // KEYCODE_STEM_1 is present on the device\n}\n```\n\nHandle button presses\n---------------------\n\n\nThere are a number of possible button keycodes that your app can handle:\n\n- `KEYCODE_STEM_1`\n- `KEYCODE_STEM_2 `\n- `KEYCODE_STEM_3`\n\n\nYour app can receive these key codes and convert them to specific in-app actions.\n\n\nTo handle a button press, implement the\n[`onKeyDown()`](/reference/android/app/Activity#onKeyDown(int,%20android.view.KeyEvent)) method.\n\n\nFor example, this implementation responds to button presses to control\nactions in an app: \n\n### Kotlin\n\n```kotlin\n// Activity\noverride fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {\n return if (event.repeatCount == 0) {\n when (keyCode) {\n KeyEvent.KEYCODE_STEM_1 -\u003e {\n // Do stuff\n true\n }\n KeyEvent.KEYCODE_STEM_2 -\u003e {\n // Do stuff\n true\n }\n KeyEvent.KEYCODE_STEM_3 -\u003e {\n // Do stuff\n true\n }\n else -\u003e {\n super.onKeyDown(keyCode, event)\n }\n }\n } else {\n super.onKeyDown(keyCode, event)\n }\n}\n```\n\n### Java\n\n```java\n@Override\n// Activity\npublic boolean onKeyDown(int keyCode, KeyEvent event){\n if (event.getRepeatCount() == 0) {\n if (keyCode == KeyEvent.KEYCODE_STEM_1) {\n // Do stuff\n return true;\n } else if (keyCode == KeyEvent.KEYCODE_STEM_2) {\n // Do stuff\n return true;\n } else if (keyCode == KeyEvent.KEYCODE_STEM_3) {\n // Do stuff\n return true;\n }\n }\n return super.onKeyDown(keyCode, event);\n}\n```\n\nDetermine the button positions\n------------------------------\n\n\nThe AndroidX Library provides two methods that describe the location of a button:\n\n- [`WearableButtons.getButtonLabel()`](/reference/android/support/wearable/input/WearableButtons#getButtonLabel(android.content.Context, int)) returns a localized string describing the general placement of the button on the device.\n- [`WearableButtons.getButtonIcon()`](/reference/android/support/wearable/input/WearableButtons#getButtonIcon(android.content.Context, int)) returns an icon representing the general placement of the button on the device.\n\n**Note:** We recommend that you avoid using textual descriptors\nwhen describing buttons and their functions. Use visual indicators instead. However, there may\nbe some cases where describing a button makes more sense.\n\n\nThe previous methods were designed for simple descriptions. If these APIs don't suit your app's\nneeds, you can also use the `WearableButtons.getButtonInfo()` API to get the\nlocation of the button on the screen and handle it in a more customized way. For more\ninformation on the APIs, see the\n[Wear API reference](/reference/android/support/wearable/input/package-summary)."]]