الأزرار الخارجية
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يحتوي الجهاز القابل للارتداء عادةً على عدة أزرار مادية، تُعرَف أيضًا باسم _stems_. نظام Wear OS
تحتوي الأجهزة دائمًا على زر واحد على الأقل: زر التشغيل. أكثر من ذلك، صفر أو أكثر
قد تكون أزرار متعددة الوظائف موجودة.
يمكنك في تطبيقك تعيين أزرار متعددة الوظائف للإجراءات. على سبيل المثال، أحد تطبيقات اللياقة البدنية
بدء تمرين أو إيقافه مؤقتًا باستخدام أزرار متعددة الوظائف:
ملاحظة: يحتفظ Wear OS 3.0 بزرَّين لنظام التشغيل، بينما
يعمل Wear OS 2.0 على الاحتفاظ بنموذج واحد فقط. يقلل هذا من عدد الأزرار التي يمكنك تخصيصها
الإجراءات بشأنها.
يصف هذا الدليل طريقة استرداد معلومات عن الأزرار المتعدّدة الوظائف المتوفّرة على
أحد الأجهزة وكيفية معالجة ضغطات الأزرار.
للحصول على معلومات إضافية حول الأزرار على أحد الأجهزة، يمكنك استخدام واجهة برمجة التطبيقات المحددة في
مكتبة إدخال Wear AndroidX. إضافة
الاعتمادية التالية في ملف build.gradle
لوحدة تطبيقك:
dependencies {
implementation "androidx.wear:wear-input:1.0.0"
}
لمعرفة عدد الأزرار المتاحة على الجهاز، استخدِم
WearableButtons.getButtonCount()
. تتضمن هذه الطريقة زر التشغيل،
لذلك إذا أرجعت الطريقة قيمة أكبر من واحد، فستتوفر أزرار متعددة الوظائف
للاستخدام. للحصول على عدد دقيق للأزرار متعددة الوظائف القابلة للتعيين، اطرح
واحد من العدد، لأن الزر الأول هو دائمًا زر التشغيل.
يتم ربط كل زر بالعدد الثابت 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 طريقتين تصفان مكان الزر:
ملاحظة: ننصحك بتجنّب استخدام أدوات الوصف النصية.
عند وصف الأزرار ووظائفها. يمكنك استخدام المؤشرات المرئية بدلاً من ذلك. ومع ذلك، قد يكون هناك
هي بعض الحالات التي يكون فيها وصف الزر أكثر منطقية.
تم تصميم الطرق السابقة للأوصاف البسيطة. إذا لم تكن واجهات برمجة التطبيقات هذه مناسبة لتطبيقك
يمكنك أيضًا استخدام واجهة برمجة تطبيقات WearableButtons.getButtonInfo()
للحصول على
موضع الزرّ على الشاشة والتعامل معه بطريقة أكثر تخصيصًا. لمزيد من المعلومات،
معلومات عن واجهات برمجة التطبيقات، راجع
مرجع Wear API
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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)."]]