تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تحتوي بعض أجهزة Wear OS على زر جانبي قابل للتدوير. عندما يحول المستخدم
ينقل العرض الحالي لتطبيقك إلى الأعلى أو الأسفل. يسمى هذا النوع من الإدخال
إدخال البيانات عبر وحدة تحكُّم دورانية.
ملاحظة: يشير هذا الدليل في المقام الأول إلى التعامل مع بيانات الإدخال عبر وحدة تحكُّم دورانية باستخدام
واجهات المستخدم القائمة على العرض. لمزيد من المعلومات حول التعامل مع البيانات عبر وحدة تحكُّم دورانية باستخدام ميزة "إنشاء" في نظام Wear OS، يُرجى الاطّلاع على
إدخال البيانات عبر وحدة تحكُّم دورانية في ميزة "إنشاء":
العديد من الحاويات القابلة للتمرير، مثل
ScrollView,
ListView,
HorizontalScrollView,
وWearableRecyclerView،
دعم طريقة إدخال البيانات عبر وحدة تحكُّم دورانية إذا كانت لديهم تفاصيل بدون الحاجة إلى أجهزة Wear
الرمز الخاص بنظام التشغيل.
وهو شرط أساسي مهمّ، لأنّه في Android 9 (مستوى واجهة برمجة التطبيقات)
28) أو أعلى، لا يتم التركيز ضمنيًا على المشاهدات.
أفضل الممارسات للتركيز
للاستجابة إلى أحداث الإدخال بالتناوب، يجب التركيز على الحاوية القابلة للتمرير.
لا تظهر أحداث الإدخال عبر الدوران فقاعات في العرض
التسلسل الهرمي. وإذا لم تكن هناك طريقة عرض مركَّز عليها، أو إذا كانت طريقة العرض المركّزة تعرض false من
View.onGenericMotionEvent(),
فسيتم إرسال الحدث إلى
Activity.onGenericMotionEvent()
في ما يلي أفضل الممارسات المتعلّقة بالاستجابة لأحداث الإدخال العشوائية:
ضع في اعتبارك أنه كإعداد افتراضي، يُعد بدء نشاط ما أو حتى النقر على
إلا أن طريقة العرض لا تعطيه التركيز، حتى لو كانت
يمكن التركيز عليه. للتركيز على طريقة العرض، يجب أن تستخدم طريقة العرض
<requestFocus />
أو الاتصال يدويًا بـ View.requestFocus().
وضع علامة "قابلة للتركيز" على طرق العرض المخصّصة القابلة للتمرير باستخدام كل من "android:focusable="true""
وandroid:focusableInTouchMode="true".
إذا كان العرض القابل للتمرير مرفقًا بعد Activity.onCreate():
على سبيل المثال، انتظار انتهاء طلب الشبكة قبل إنشاء واجهة المستخدم،
requestFocus() بعد الإرفاق.
إذا كان العرض القابل للتمرير هو INVISIBLE مبدئيًا
أو GONE،
طلب "requestFocus()" عند ضبطه على
VISIBLE
إذا كان نشاطك يتضمّن عدة طرق عرض قابلة للتمرير، اختَر طريقة عرض للتركيز عليها باستخدام
<requestFocus />
. لا تتوفّر إمكانية التمرير المدمَج مع الزر الجانبي الذي يتم تدويره.
إذا كانت واجهة المستخدم تحتوي على طرق عرض أخرى يتمّ التركيز عليها عندما ينقر المستخدم
يتفاعل معها - على سبيل المثال،
InputText، تمنح المستخدم طريقة لاستعادة التركيز إلى العرض القابل للتمرير
عند فقدان التركيز من خلال الاستماع إلى النقرات على طريقة العرض القابلة للتمرير، ثم إجراء مكالمة
ردّ requestFocus().
سلوك تبديل مخصّص
إذا كان العرض القابل للتمرير لا يتيح في الأصل تمرير الإدخال باستخدام وحدة تحكُّم دورانية، أو إذا كنت تريد
استخدام طريقة إدخال البيانات عبر وحدة تحكُّم دورانية لشيء آخر غير التمرير، مثل
التكبير والتصغير أو تدوير القرص - يمكنك التعامل مع أحداث التمرير
نفسك. تذكر التأكد من أن العرض يكتسب التركيز، وإلا
فلن يتم تنفيذ الأحداث.
myView.setOnGenericMotionListener{v,ev->
if(ev.action==MotionEvent.ACTION_SCROLL&&
ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)){// Don't forget the negation herevaldelta=-ev.getAxisValue(MotionEventCompat.AXIS_SCROLL)*ViewConfigurationCompat.getScaledVerticalScrollFactor(ViewConfiguration.get(context),context)// Swap these axes to scroll horizontally insteadv.scrollBy(0,delta.roundToInt())true}else{false}}
Java
myView.setOnGenericMotionListener(newView.OnGenericMotionListener(){@OverridepublicbooleanonGenericMotion(Viewv,MotionEventev){if(ev.getAction()==MotionEvent.ACTION_SCROLL&&
ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)){// Don't forget the negation herefloatdelta=-ev.getAxisValue(MotionEventCompat.AXIS_SCROLL)*ViewConfigurationCompat.getScaledVerticalScrollFactor(ViewConfiguration.get(context),context);// Swap these axes to scroll horizontally insteadv.scrollBy(0,Math.round(delta));returntrue;}returnfalse;}});
الاختبار باستخدام محاكي
استخدام محاكي Android لمحاكاة الإدخال عبر وحدة تحكُّم دورانية
على جهاز Wear OS. افتح تطبيق Wear OS على المحاكي لتشغيله.
مشروعك أو اسحب
APK على المحاكي لتثبيته.
لاختبار الإدخال عبر وحدة تحكُّم دورانية على المحاكي:
من مدير حزمة تطوير البرامج (SDK)، استخدِم علامة التبويب أدوات حزمة تطوير البرامج (SDK) من أجل
الحصول على الإصدار 26.0.3 من Android Emulator أو إصدار أحدث.
في "استوديو Android"، اختَر الأدوات >.
Android > مدير AVDإنشاء جهاز Wear جديد مع واجهة برمجة التطبيقات 25 أو
أعلى.
انقر على القائمة الكاملة للنقاط الثلاث أسفل شريط أدوات المحاكي. انقر على
انقر على علامة تبويب إدخال البيانات عبر وحدة تحكُّم دورانية في النافذة الجديدة لفتح واجهة الإدخال الدوّارة وتجربة إدخال البيانات عبر وحدة تحكُّم دورانية.
التمرير بين الإدخالات.
يعرض الفيديو التالي طريقة إدخال البيانات عبر وحدة تحكُّم دورانية في المحاكي:
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ 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,["# Rotary input\n\nSome Wear OS devices contain a physical *rotating side button* . When the user turns the\nbutton, it scrolls your app's current view up or down. This type of input is called\n*rotary input*.\n\n**Note:** This guide refers primarily to handling rotary input using\nView-based UIs. For more information on handling rotary input using Compose for Wear OS, see\n[Rotary input on Compose](/training/wearables/compose/rotary-input).\n\nMany scrollable containers, like\n[ScrollView](/reference/android/widget/ScrollView),\n[ListView](/reference/android/widget/ListView),\n[HorizontalScrollView](/reference/android/widget/HorizontalScrollView),\nand [WearableRecyclerView](/reference/androidx/wear/widget/WearableRecyclerView),\nsupport rotary input if they have focus without requiring any Wear\nOS-specific code.\nHaving focus is an important prerequisite, because on Android 9 (API level\n28) and higher, views don't implicitly receive focus.\n\nFocus best practices\n--------------------\n\n\nTo respond to rotary input events, a scrollable container must have focus.\nRotary input events don't bubble up the view\nhierarchy. If there is no focused view, or if the focused view returns `false` from\n[View.onGenericMotionEvent()](/reference/android/view/View#onGenericMotionEvent(android.view.MotionEvent)),\nthen the event is sent to\n[Activity.onGenericMotionEvent()](/reference/android/app/Activity#onGenericMotionEvent(android.view.MotionEvent)).\n\n\nThe following are best practices around responding to rotary input events:\n\n- Bear in mind that, by default, launching an activity or even tapping on a view does not give it focus, even if it is focusable. To give your view focus, the view must use the [<requestFocus /\u003e](/guide/topics/resources/layout-resource) tag or manually call [View.requestFocus()](/reference/android/view/View#requestFocus()).\n- Mark custom scrollable views as focusable using both `android:focusable=\"true\"` and `android:focusableInTouchMode=\"true\"`.\n- If your scrollable view is attached after [Activity.onCreate()](/reference/android/app/Activity#onCreate(android.os.Bundle))---for example, waiting for a network request to finish before building your UI, call `requestFocus()` after attaching it.\n- If your scrollable view is initially [INVISIBLE](/reference/android/view/View#INVISIBLE) or [GONE](/reference/android/view/View#GONE), call `requestFocus()` when you set it to [VISIBLE](/reference/android/view/View#VISIBLE).\n- If your activity contains multiple scrollable views, choose one to focus using the [<requestFocus /\u003e](/guide/topics/resources/layout-resource) tag. Nested scrolling is not supported with the rotating side button.\n- If your UI contains some other view that takes focus when the user interacts with it---for example, an `InputText`, give the user a way to restore focus to the scrollable view if it loses focus by listening for taps on the scrollable view and calling `requestFocus()` in response.\n\nCustom rotating behavior\n------------------------\n\nIf your scrollable view doesn't natively support rotary input scrolling, or if you want to\nuse your rotary input for something other than scrolling---such as to\nzoom in and out or to turn dials---you can handle the scroll events\nyourself. Remember to make sure your view gains focus, otherwise\nthe events will not come through.\n\nThe following code snippet shows how to use [MotionEvent](/reference/android/view/MotionEvent),\n[InputDeviceCompat](/reference/kotlin/androidx/core/view/InputDeviceCompat),\nand [ViewConfigurationCompat](/reference/androidx/core/view/ViewConfigurationCompat)\nto add custom scrolling to your view: \n\n### Kotlin\n\n```kotlin\nmyView.setOnGenericMotionListener { v, ev -\u003e\n if (ev.action == MotionEvent.ACTION_SCROLL &&\n ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)\n ) {\n // Don't forget the negation here\n val delta = -ev.getAxisValue(MotionEventCompat.AXIS_SCROLL) *\n ViewConfigurationCompat.getScaledVerticalScrollFactor(\n ViewConfiguration.get(context), context\n )\n // Swap these axes to scroll horizontally instead\n v.scrollBy(0, delta.roundToInt())\n true\n } else {\n false\n }\n}\n```\n\n### Java\n\n```java\nmyView.setOnGenericMotionListener(new View.OnGenericMotionListener() {\n @Override\n public boolean onGenericMotion(View v, MotionEvent ev) {\n if (ev.getAction() == MotionEvent.ACTION_SCROLL &&\n ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)\n ) {\n // Don't forget the negation here\n float delta = -ev.getAxisValue(MotionEventCompat.AXIS_SCROLL) *\n ViewConfigurationCompat.getScaledVerticalScrollFactor(\n ViewConfiguration.get(context), context\n );\n\n // Swap these axes to scroll horizontally instead\n v.scrollBy(0, Math.round(delta));\n\n return true;\n }\n return false;\n }\n});\n```\n\nTest using an emulator\n----------------------\n\nUse the [Android Emulator](/studio/run/emulator#about) to simulate rotary input\nscrolling on a Wear device. Launch your Wear app on the emulator to run\nyour project or drag an\nAPK file onto the emulator to install it.\n\nTo test the rotary input on the emulator:\n\n1. From the [SDK manager](/tools/help/sdk-manager), use the **SDK tools** tab to get Android Emulator 26.0.3 or higher.\n2. In Android Studio, select **Tools \\\u003e\n Android \\\u003e AVD Manager** . [Create a new Wear device](/studio/run/managing-avds#createavd) with API 25 or higher.\n3. [Run the emulator from Android Studio](/studio/run/emulator#runningapp).\n4. Click the three-dot overflow menu at the bottom of the emulator toolbar. Click the **Rotary input** tab in the new window to open the rotary input interface and try rotary input scrolling.\n\nThe following video shows rotary input in the emulator:"]]