ปุ่มบนตัวเครื่อง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
อุปกรณ์ที่สวมใส่ได้มักมีปุ่มหลายปุ่ม หรือที่เรียกว่า _stems_ Wear OS
อุปกรณ์จะมีปุ่มอย่างน้อย 1 ปุ่ม นั่นคือปุ่มเปิด/ปิด นอกเหนือจากนั้น หรือมากกว่า
อาจมีปุ่มหลายฟังก์ชัน
คุณกำหนดปุ่มหลายฟังก์ชันให้กับการทำงานในแอปได้ เช่น แอปฟิตเนส
อาจเริ่มหรือหยุดการออกกำลังกายชั่วคราวโดยใช้ปุ่มหลายฟังก์ชัน ดังนี้
หมายเหตุ: Wear OS 3.0 สงวนปุ่มไว้ 2 ปุ่มสำหรับระบบปฏิบัติการ
Wear OS 2.0 จะสงวนไว้เพียงรายการเดียวเท่านั้น ซึ่งจะลดจำนวนปุ่มที่คุณกำหนดได้
การดำเนินการ
คู่มือนี้จะอธิบายวิธีเรียกดูข้อมูลเกี่ยวกับปุ่มหลายฟังก์ชันที่พร้อมใช้งานบน
อุปกรณ์และวิธีประมวลผลการกดปุ่ม
หากต้องการดูข้อมูลเพิ่มเติมเกี่ยวกับปุ่มต่างๆ ในอุปกรณ์ ให้ใช้ API ที่ระบุไว้ใน
Wear Input ไลบรารี AndroidX เพิ่ม
ทรัพยากร Dependency ต่อไปนี้ในไฟล์ build.gradle
ของโมดูลแอป
dependencies {
implementation "androidx.wear:wear-input:1.0.0"
}
หากต้องการดูจำนวนปุ่มที่มีอยู่ในอุปกรณ์ ให้ใช้
WearableButtons.getButtonCount()
วิธีการนี้รวมถึงปุ่มเปิด/ปิด
ดังนั้นถ้าเมธอดแสดงค่ามากกว่า 1 ก็จะมีปุ่มหลายฟังก์ชันพร้อมใช้งาน
สำหรับการใช้งาน หากต้องการจำนวนที่ถูกต้องของปุ่มหลายฟังก์ชันที่กำหนดได้ ให้ลบด้วย
ปุ่มหนึ่งจากการนับ เนื่องจากปุ่มแรกจะเป็นปุ่มเปิด/ปิดเสมอ
แต่ละปุ่มจะแมปกับค่าคงที่ 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 มีวิธีอธิบายตำแหน่งของปุ่ม 2 วิธี ดังนี้
หมายเหตุ: ขอแนะนำให้หลีกเลี่ยงการใช้ข้อบ่งชี้แบบข้อความ
เมื่ออธิบายปุ่มและฟังก์ชัน ใช้สัญญาณบอกสถานะภาพแทน อย่างไรก็ตาม อาจมี
ในบางกรณีที่การอธิบายปุ่มจะดูเหมาะสมกว่า
โดยวิธีการก่อนหน้านี้ได้รับการออกแบบมาสำหรับคำอธิบายที่เรียบง่าย หาก API เหล่านี้ไม่เหมาะกับแอปของคุณ
คุณยังใช้ WearableButtons.getButtonInfo()
API เพื่อรับ
ตำแหน่งของปุ่มบนหน้าจอและจัดการด้วยวิธีที่ปรับแต่งได้มากขึ้น สำหรับข้อมูลเพิ่มเติม
เกี่ยวกับ API โปรดดู
เอกสารอ้างอิง Wear API
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-26 UTC"],[],[],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)."]]