[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Support keyboard navigation\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to use touch and input in Compose. \n[Focus →](/develop/ui/compose/touch-input/focus) \n\nIn addition to soft input methods---such as on-screen\nkeyboards---Android supports physical keyboards attached to the device. A\nkeyboard offers a convenient mode for text input and a way for users to\nnavigate and interact with your app. Although most hand-held devices such as\nphones use touch as the primary mode of interaction, tablets and similar\ndevices are popular, and many users like to attach keyboard accessories to\nthem.\n\nAs more Android-powered devices offer this kind of experience, it's important\nthat you optimize your app to support interaction through a keyboard. This\ndocument describes how you can improve navigation with a keyboard.\n| **Note:** Supporting directional navigation in your app is also an important part of helping ensure your app is [accessible](/guide/topics/ui/accessibility/apps) to users who don't navigate using visual cues. Fully supporting directional navigation in your app can also help you automate [user interface testing](/tools/testing/testing_ui) with tools like [UiAutomator](/tools/help/uiautomator).\n\nTest your app\n-------------\n\nUsers might already be able to navigate your app using a keyboard, because\nthe Android system enables most of the necessary behaviors by default.\n\nAll interactive widgets provided by the Android framework---such as\n[Button](/reference/android/widget/Button) and\n[EditText](/reference/android/widget/EditText)---are\nfocusable. This means users can navigate with control devices such as a D-pad or\nkeyboard, and each widget glows or otherwise changes its appearance when it\ngains input focus.\n\nTo test your app, perform the following procedure:\n\n1. Install your app on a device that offers a hardware keyboard. If you don't have a hardware device with a keyboard, connect a Bluetooth\n keyboard or a USB keyboard.\n\n You can also use the Android emulator:\n 1. In the AVD Manager, either click **New Device** or select an existing profile and click **Clone**.\n 2. In the window that appears, ensure **Keyboard** and **DPad** are enabled.\n2. To test your app, use only the \u003ckbd\u003eTab\u003c/kbd\u003e key to navigate through your UI. Make sure each UI control gets focus as expected.\n\n Look for any instances in which the focus moves in an unexpected\n manner.\n3. Start again from the beginning of your app and navigate through your UI using direction controls like the arrow keys on the keyboard. From each focusable element in your UI, press \u003ckbd\u003eUp\u003c/kbd\u003e, \u003ckbd\u003eDown\u003c/kbd\u003e, \u003ckbd\u003eLeft\u003c/kbd\u003e, and \u003ckbd\u003eRight\u003c/kbd\u003e.\n\n Look for any instances in which the focus moves in an unexpected\n manner.\n\nIf you encounter any instances where navigating with the \u003ckbd\u003eTab\u003c/kbd\u003e key\nor direction controls doesn't do what you expect, specify where the focus must\nbe in your layout, as discussed in the following sections.\n\nHandle tab navigation\n---------------------\n\nWhen a user navigates your app using the keyboard \u003ckbd\u003eTab\u003c/kbd\u003e key, the\nsystem passes input focus between elements based on the order in which they\nappear in the layout. If you use a relative layout, for example, and the order\nof elements on the screen is different than the order in the file, then you\nmight need to manually specify the focus order.\n\nFor example, in the following layout, two buttons are aligned to the right\nside, and a text field is aligned to the left of the second button. To pass\nfocus from the first button to the text field and then to the second button, the\nlayout needs to explicitly define the focus order for each of the focusable\nelements with the\n[`android:nextFocusForward`](/reference/android/view/View#attr_android:nextFocusForward)\nattribute. \n\n```xml\n\u003candroidx.constraintlayout.widget.ConstraintLayout ...\u003e\n \u003cButton\n android:id=\"@+id/button1\"\n android:nextFocusForward=\"@+id/editText1\"\n app:layout_constraintRight_toRightOf=\"parent\"\n app:layout_constraintTop_toTopOf=\"parent\"\n ... /\u003e\n \u003cButton\n android:id=\"@+id/button2\"\n android:nextFocusForward=\"@+id/button1\"\n app:layout_constraintStart_toStartOf=\"parent\"\n app:layout_constraintTop_toBottomOf=\"@id/button1\"\n ... /\u003e\n \u003cEditText\n android:id=\"@id/editText1\"\n android:nextFocusForward=\"@+id/button2\"\n app:layout_constraintBottom_toBottomOf=\"@+id/button2\"\n app:layout_constraintRight_toLeftOf=\"@id/button2\n ... /\u003e\n ...\n\u003c/androidx.constraintlayout.widget.ConstraintLayout\u003e\n```\n\nNow, instead of the focus moving from `button1` to\n`button2` and then `editText1`, it appropriately moves\naccording to the appearance on the screen: from `button1` to\n`editText1` and then `button2`.\n\nHandle directional navigation\n-----------------------------\n\nUsers can also navigate your app using the arrow keys on a keyboard, which\nbehaves the same as when navigating with a D-pad or trackball. The system\nprovides a \"best guess\" for which view to give focus to in a given direction\nbased on the layout of the views on screen. However, sometimes the system\nguesses wrong.\n\nIf the system doesn't pass focus to the appropriate view when navigating in a\ngiven direction, specify which view must receive focus with the following\nattributes:\n\n- [`android:nextFocusUp`](/reference/android/view/View#attr_android:nextFocusUp)\n- [`android:nextFocusDown`](/reference/android/view/View#attr_android:nextFocusDown)\n- [`android:nextFocusLeft`](/reference/android/view/View#attr_android:nextFocusLeft)\n- [`android:nextFocusRight`](/reference/android/view/View#attr_android:nextFocusRight)\n\nEach attribute designates the next view to receive focus when the user\nnavigates in that direction, as specified by the view ID. This is shown in the\nfollowing example: \n\n```xml\n\u003cButton\n android:id=\"@+id/button1\"\n android:nextFocusRight=\"@+id/button2\"\n android:nextFocusDown=\"@+id/editText1\"\n ... /\u003e\n\u003cButton\n android:id=\"@id/button2\"\n android:nextFocusLeft=\"@id/button1\"\n android:nextFocusDown=\"@id/editText1\"\n ... /\u003e\n\u003cEditText\n android:id=\"@id/editText1\"\n android:nextFocusUp=\"@id/button1\"\n ... /\u003e\n```\n| **Note:** Some Android views override the default navigation keys. For example, [`EditText`](/reference/android/widget/EditText) overrides the arrow keys to provide navigation within the inserted text.\n\nAdditional resources\n--------------------\n\nRefer to the following related resources:\n\n- [Build accessible apps](/training/accessibility)"]]