Utilizzare i gesti del polso su Wear
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
I gesti del polso possono consentire interazioni rapide con una sola mano con la tua app
quando un touchscreen non è pratico.
Ad esempio, un utente può scorrere
tramite le notifiche con una mano mentre tiene una tazza d'acqua con
e l'altro. Altri casi d'uso per i gesti del polso sono:
- In un'app di jogging, la navigazione tra schermate verticali che mostrano
passi effettuati, tempo trascorso e ritmo attuale
- In un'app di viaggio, scorrere le informazioni su un volo e sul gate
- In un'app di notizie, scorrere gli articoli
Per rivedere i gesti del polso su un orologio
dispositivo, verifica che i gesti siano
attivata selezionando Impostazioni > Funzionalità avanzate > Gesti > Gesti del polso
Attivato. Quindi completa la
Il tutorial sui gesti sull'orologio selezionando Avvia tutorial.
Nota: scuotere il polso rappresenta il gesto Indietro a livello di sistema o di annullamento.
e non è disponibile per la personalizzazione delle app.
I gesti del polso possono essere utilizzati nei seguenti modi, descritti in questa guida:
Ogni gesto del polso è mappato a una costante int
dalla
KeyEvent
come illustrato nella tabella seguente:
Gesto
|
Evento chiave
|
Descrizione
|
Ruota il polso verso l'esterno
|
KEYCODE_NAVIGATE_NEXT
|
Questo codice chiave va all'elemento successivo.
|
Ruota il polso verso l'interno
|
KEYCODE_NAVIGATE_PREVIOUS
|
Questo codice chiave va all'elemento precedente.
|
Usa un layout curvo per supportare i gesti del polso
La classe
WearableRecyclerView
fornisce una curva
per gli elenchi e supporta automaticamente
gesti del polso. La classe ha azioni predefinite per le occorrenze di
gesti del polso quando la visualizzazione è a fuoco. Per informazioni sull'utilizzo
la WearableRecyclerView
classe, vedi Creare elenchi su Wear OS. Inoltre, vedi le
Best practice di questa guida.
Nota: il corso WearableRecyclerView
sostituisce una classe simile,
deprecato nella libreria di supporto Wearable.
Anche se usi un WearableRecyclerView
, potrebbe essere utile usare
costanti da KeyEvent
. Le azioni predefinite possono essere sostituite creando sottoclassi del tipo
WearableRecyclerView
e reimplementando il
Chiamata onKeyDown()
. Il comportamento può essere completamente disattivato
mediante setEnableGestureNavigation(false)
.
Per ulteriori informazioni, vedi
Consente di gestire le azioni da tastiera.
Utilizza direttamente gli eventi chiave
Puoi utilizzare gli eventi chiave al di fuori di un
WearableRecyclerView
per attivare nuove azioni in risposta al gesto
eventi. Soprattutto, questi eventi di gesti vengono riconosciuti quando un dispositivo è in
attiva e vengono pubblicati come tutti gli eventi chiave.
Una classe relativa all'interazione dell'utente, ad esempio View
o un
Activity
, che implementa
KeyEvent.Callback
può ascoltare eventi chiave correlati a
gesti del polso come per qualsiasi altro evento chiave. Framework Android
chiama View
o Activity
che ha
concentrati sugli eventi chiave. Per i gesti, onKeyDown()
viene chiamato quando si verificano dei gesti.
Ad esempio, un'app può eseguire l'override delle azioni predefinite in un View
o Activity
che implementa KeyEvent.Callback
nel seguente modo:
Kotlin
class GesturesActivity : Activity() {
/* KeyEvent.Callback */
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
return when (keyCode) {
KeyEvent.KEYCODE_NAVIGATE_NEXT ->
// Do something that advances a user View to the next item in an ordered list.
moveToNextItem()
KeyEvent.KEYCODE_NAVIGATE_PREVIOUS ->
// Do something that advances a user View to the previous item in an ordered list.
moveToPreviousItem()
else -> {
// If you did not handle it, let it be handled by the next possible element as determined
// by the Activity.
super.onKeyDown(keyCode, event)
}
}
}
/** Shows the next item in the custom list. */
private fun moveToNextItem(): Boolean {
...
// Return true if handled successfully, otherwise return false.
return false
}
/** Shows the previous item in the custom list. */
private fun moveToPreviousItem(): Boolean {
...
// Return true if handled successfully, otherwise return false.
return false
}
}
Java
public final class GesturesActivity extends Activity {
@Override /* KeyEvent.Callback */
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_NAVIGATE_NEXT:
// Do something that advances a user View to the next item in an ordered list.
return moveToNextItem();
case KeyEvent.KEYCODE_NAVIGATE_PREVIOUS:
// Do something that advances a user View to the previous item in an ordered list.
return moveToPreviousItem();
}
// If you did not handle it, let it be handled by the next possible element as determined by the Activity.
return super.onKeyDown(keyCode, event);
}
/** Shows the next item in the custom list. */
private boolean moveToNextItem() {
boolean handled = false;
...
// Return true if handled successfully, otherwise return false.
return handled;
}
/** Shows the previous item in the custom list. */
private boolean moveToPreviousItem() {
boolean handled = false;
...
// Return true if handled successfully, otherwise return false.
return handled;
}
}
Best practice
- Leggi
KeyEvent
e
KeyEvent.Callback
pagine per la pubblicazione degli eventi chiave
View
e Activity
.
- Mantieni un approccio direzionale coerente: usa "ruota il polso verso l'esterno" della
e poi "ruota il polso verso l'interno" per i precedenti.
- Usa un tocco parallelo per un gesto.
- Fornisci un feedback visivo.
- Non utilizzare un codice chiave per implementare funzionalità che potrebbero
controintuitivo al resto del sistema. Ad esempio, non utilizzare
KEYCODE_NAVIGATE_NEXT
per annullare un'azione o navigare nel
asse sinistro-destro con rulli.
- Non intercettare gli eventi chiave sugli elementi che non fanno parte del
dell'interfaccia utente, ad esempio le visualizzazioni fuori schermo o parzialmente
in questione. Questo vale per qualsiasi evento chiave.
- Non reinterpretare i gesti di rotazione ripetuti in un tuo nuovo gesto.
Il dispositivo potrebbe essere in conflitto con lo "scuotimento del polso" del sistema gesto.
Affinché una vista possa ricevere gli eventi chiave dei gesti, deve avere
focus; vedi
View.setFocusable()
.
Poiché i gesti vengono considerati come eventi chiave,
attivano una transizione dalla "modalità touch" che potrebbero generare risposte impreviste
cose nuove. Dato che gli utenti possono alternare le funzionalità touch e
gesti, potrebbe essere necessario il metodo
View::setFocusableInTouchmode()
. In alcuni
casi, potrebbe essere necessario usare anche
setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS)
così
che, quando lo stato attivo cambia dopo una modifica alla o dalla modalità tocco, il
con la vista desiderata.
- Utilizza
requestFocus()
e
clearFocus()
attentamente:
.
- Quando chiami il numero
requestFocus()
, assicurati che sia appropriato per il
per mettere a fuoco. Se la visualizzazione è fuori schermo o coperta da un'altra visualizzazione,
possono verificarsi sorprese quando i gesti attivano richiamate.
- Il metodo
clearFocus()
avvia una ricerca di impostazione dello stato attivo per trovarne un altro
vista adatta. A seconda della gerarchia delle visualizzazioni, questa ricerca potrebbe
non banali. Potrebbe anche assegnare lo stato attivo
a una vista che non ti aspetti di mettere in evidenza.
Gli eventi chiave vengono pubblicati per primi nella vista con lo stato attivo nella vista
nella gerarchia. Se la vista con lo stato attivo non gestisce l'evento, cioè restituisce
false
: l'evento non viene pubblicato nella vista principale, nemmeno
se può ricevere lo stato attivo e ha un valore
KeyListener
. Piuttosto, l'evento viene pubblicato nell'attività corrente.
che mostra la gerarchia di visualizzazione con lo stato attivo.
Pertanto, potrebbe essere necessario
rilevare tutti gli eventi al livello superiore e poi trasmettere i codici pertinenti.
In alternativa, puoi creare sottoclassi dell'attività e sostituire la
Metodo
dispatchKeyEvent(KeyEvent event)
per intercettare le chiavi
quando necessario o quando non vengono gestiti
strati inferiori.
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-07-26 UTC.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-07-26 UTC."],[],[],null,["# Use wrist gestures on Wear\n\nWrist gestures can enable quick, one-handed interactions with your app\nwhen a touch screen is inconvenient.\n\n\nFor example, a user can scroll\nthrough notifications with one hand while holding a cup of water with the\nother. Other use cases for wrist gestures include the following:\n\n- In a jogging app, navigating through vertical screens that show the steps taken, time elapsed, and current pace\n- In a travel app, scrolling through flight and gate information\n- In a news app, scrolling through articles\n\n\nTo review the [wrist gestures](https://support.google.com/androidwear/answer/6312406) on a watch\ndevice, confirm that gestures are\nturned on by selecting **Settings \\\u003e Advanced features \\\u003e Gestures \\\u003e Wrist Gestures\nOn** . Then complete the\nGestures tutorial on the watch by selecting **Launch Tutorial**.\n\n\n**Note:** Shaking the wrist is the system-wide back or undo gesture\nand is not available for apps to customize.\n\n\nWrist gestures can be used in the following ways, as described in this guide:\n\n- With a [curved layout](#using_wrv), which has predefined gesture actions\n- By using [key events directly](#using_key_events) to define new user actions\n\n\nEach wrist gesture is mapped to an `int` constant from the\n[KeyEvent](/reference/android/view/KeyEvent)\nclass, as shown in the following table:\n\n| Gesture | KeyEvent | Description |\n|-----------------|-------------------------------------------------------------------------------------------|------------------------------------------|\n| Flick wrist out | [`KEYCODE_NAVIGATE_NEXT`](/reference/android/view/KeyEvent#KEYCODE_NAVIGATE_NEXT) | This key code goes to the next item. |\n| Flick wrist in | [`KEYCODE_NAVIGATE_PREVIOUS`](/reference/android/view/KeyEvent#KEYCODE_NAVIGATE_PREVIOUS) | This key code goes to the previous item. |\n\nUse a curved layout to support wrist gestures\n---------------------------------------------\n\n\nThe [WearableRecyclerView](/reference/androidx/wear/widget/WearableRecyclerView) class provides a curved\nlayout for lists and automatically supports\nwrist gestures. The class has predefined actions for occurrences of\nwrist gestures when the view has focus. For information about using\nthe `WearableRecyclerView` class, see [Create lists on Wear OS](/training/wearables/ui/lists). Also, see the\n[Best practices](#best_practices) section of this guide.\n\n\n**Note:** The `WearableRecyclerView` class replaces a similar,\n[deprecated](/training/wearables/ui/wear-ui-library#deprecations) class in the Wearable Support Library.\n\n\nEven if you use a `WearableRecyclerView`, you might want to use\nconstants from the [KeyEvent](/reference/android/view/KeyEvent)\nclass. The predefined actions can be overridden by subclassing the\n`WearableRecyclerView` and re-implementing the\n`onKeyDown()` callback. The behavior can be disabled entirely\nby using [`setEnableGestureNavigation(false)`](/reference/android/support/wearable/view/WearableListView#setEnableGestureNavigation(boolean)).\nFor more information, see\n[Handle keyboard actions](/training/keyboard-input/commands).\n\nUse key events directly\n-----------------------\n\n\nYou can use key events outside of a [WearableRecyclerView](/reference/androidx/wear/widget/WearableRecyclerView) to trigger new actions in response to gesture\nevents. Importantly, these gesture events are recognized when a device is in\nactive mode, and they are delivered in the same way as all key events.\n\n\nA class that relates to user interaction, such as a `View` or an\n`Activity`, and that implements\n[KeyEvent.Callback](/reference/android/view/KeyEvent.Callback) can listen to key events that relate to\nwrist gestures just as it can listed to any other key event. The Android framework\ncalls the `View` or `Activity` that has\nfocus with the key events. For gestures, the `onKeyDown()`\nmethod callback is called when gestures occur.\n\n\nAs an example, an app can override predefined actions in a `View`\nor `Activity` that implements `KeyEvent.Callback` as follows: \n\n### Kotlin\n\n```kotlin\nclass GesturesActivity : Activity() {\n\n /* KeyEvent.Callback */\n override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {\n return when (keyCode) {\n KeyEvent.KEYCODE_NAVIGATE_NEXT -\u003e\n // Do something that advances a user View to the next item in an ordered list.\n moveToNextItem()\n KeyEvent.KEYCODE_NAVIGATE_PREVIOUS -\u003e\n // Do something that advances a user View to the previous item in an ordered list.\n moveToPreviousItem()\n else -\u003e {\n // If you did not handle it, let it be handled by the next possible element as determined\n // by the Activity.\n super.onKeyDown(keyCode, event)\n }\n }\n }\n\n /** Shows the next item in the custom list. */\n private fun moveToNextItem(): Boolean {\n ...\n // Return true if handled successfully, otherwise return false.\n return false\n }\n\n /** Shows the previous item in the custom list. */\n private fun moveToPreviousItem(): Boolean {\n ...\n // Return true if handled successfully, otherwise return false.\n return false\n }\n}\n```\n\n### Java\n\n```java\npublic final class GesturesActivity extends Activity {\n\n @Override /* KeyEvent.Callback */\n public boolean onKeyDown(int keyCode, KeyEvent event) {\n switch (keyCode) {\n case KeyEvent.KEYCODE_NAVIGATE_NEXT:\n // Do something that advances a user View to the next item in an ordered list.\n return moveToNextItem();\n case KeyEvent.KEYCODE_NAVIGATE_PREVIOUS:\n // Do something that advances a user View to the previous item in an ordered list.\n return moveToPreviousItem();\n }\n // If you did not handle it, let it be handled by the next possible element as determined by the Activity.\n return super.onKeyDown(keyCode, event);\n }\n\n /** Shows the next item in the custom list. */\n private boolean moveToNextItem() {\n boolean handled = false;\n ...\n // Return true if handled successfully, otherwise return false.\n return handled;\n }\n\n /** Shows the previous item in the custom list. */\n private boolean moveToPreviousItem() {\n boolean handled = false;\n ...\n // Return true if handled successfully, otherwise return false.\n return handled;\n }\n}\n```\n\nBest practices\n--------------\n\n- Review the [KeyEvent](/reference/android/view/KeyEvent) and [KeyEvent.Callback](/reference/android/view/KeyEvent.Callback) pages for the delivery of key events to your `View` and `Activity`.\n- Keep a consistent directional affordance: use \"flick wrist out\" for next and \"flick wrist in\" for previous.\n- Have a touch parallel for a gesture.\n- Provide visual feedback.\n- Don't use a keycode to implement functionality that would be counterintuitive to the rest of the system. For example, don't use `KEYCODE_NAVIGATE_NEXT` to cancel an action or to navigate the left-right axis with flicks.\n- Don't intercept the key events on elements that are not part of the user interface, such as views that are offscreen or partially covered. This is the same as for any key event.\n- Don't reinterpret repeated flick gestures into your own novel gesture. This might conflict with the system's \"shaking the wrist\" gesture.\n- For a view to receive gesture key events, it must have [focus](/reference/android/view/View#attr_android:focusable); see [`\n View.setFocusable()`](/reference/android/view/View#setFocusable(boolean)).\n\n Because gestures are treated as key events,\n they trigger a transition out of \"touch mode\" that might do unexpected\n things. Since users may alternate between using touch and\n gestures, the [`\n View::setFocusableInTouchmode()`](/reference/android/view/View#setFocusableInTouchMode(boolean)) method could be necessary. In some\n cases, it also could be necessary to use\n `setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS)` so\n that when focus changes after a change to or from touch mode, your\n intended view gets the focus.\n- Use [requestFocus()](/reference/android/view/View#requestFocus()) and [clearFocus()](/reference/android/view/View#clearFocus()) carefully:\n - When calling `requestFocus()`, make sure it's appropriate for the view to have focus. If the view is offscreen or is covered by another view, surprises can occur when gestures trigger callbacks.\n - The `clearFocus()` method initiates a focus search to find another suitable view. Depending on the view hierarchy, this search might require nontrivial computation. It can also end up assigning focus to a view you don't expect to receive focus.\n- Key events are delivered first to the view with focus in the view\n hierarchy. If the focused view does not handle the event---in other words, it returns\n `false`---the event is not delivered to the parent view, even\n if it can receive focus and has a [`\n KeyListener`](/reference/android/text/method/KeyListener). Rather, the event is delivered to the current activity\n holding the view hierarchy with focus.\n\n Therefore, it might be necessary to\n catch all events at the higher level, then pass relevant codes down.\n Alternatively, you might subclass the activity and override the\n [dispatchKeyEvent(KeyEvent event)](/reference/android/app/Activity#dispatchKeyEvent(android.view.KeyEvent)) method to intercept keys\n when necessary or handle them when they are not handled at\n lower layers."]]