跳转到相应内容

最常访问

最近访问

navigation

Handling Input Method Visibility

When input focus moves into or out of an editable text field, Android shows or hides the input method (such as the on-screen keyboard) as appropriate. The system also makes decisions about how your UI and the text field appear above the input method. For example, when the vertical space on the screen is constrained, the text field might fill all space above the input method. For most apps, these default behaviors are all that's needed.

In some cases, though, you might want to more directly control the visibility of the input method and specify how you'd like your layout to appear when the input method is visible. This lesson explains how to control and respond to the input method visibility.

Show the Input Method When the Activity Starts

Although Android gives focus to the first text field in your layout when the activity starts, it does not show the input method. This behavior is appropriate because entering text might not be the primary task in the activity. However, if entering text is indeed the primary task (such as in a login screen), then you probably want the input method to appear by default.

To show the input method when your activity starts, add the android:windowSoftInputMode attribute to the <activity> element with the "stateVisible" value. For example:

<application ... >
    <activity
        android:windowSoftInputMode="stateVisible" ... >
        ...
    </activity>
    ...
</application>

Note: If the user's device has an attached hardware keyboard, the soft input method does not appear.

Show the Input Method On Demand

If there is a method in your activity's lifecycle where you want to ensure that the input method is visible, you can use the InputMethodManager to show it.

For example, the following method takes a View in which the user should type something, calls requestFocus() to give it focus, then showSoftInput() to open the input method:

public void showSoftKeyboard(View view) {
    if (view.requestFocus()) {
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
}

Note: Once the input method is visible, you should not programmatically hide it. The system hides the input method when the user finishes the task in the text field or the user can hide it with a system control (such as with the Back button).

Specify How Your UI Should Respond

When the input method appears on the screen, it reduces the amount of space available for your app's UI. The system makes a decision as to how it should adjust the visible portion of your UI, but it might not get it right. To ensure the best behavior for your app, you should specify how you'd like the system to display your UI in the remaining space.

To declare your preferred treatment in an activity, use the android:windowSoftInputMode attribute in your manifest's <activity> element with one of the "adjust" values.

For example, to ensure that the system resizes your layout to the available space—which ensures that all of your layout content is accessible (even though it probably requires scrolling)—use "adjustResize":

<application ... >
    <activity
        android:windowSoftInputMode="adjustResize" ... >
        ...
    </activity>
    ...
</application>

You can combine the adjustment specification with the initial input method visibility specification from above:

    <activity
        android:windowSoftInputMode="stateVisible|adjustResize" ... >
        ...
    </activity>

Specifying "adjustResize" is important if your UI includes controls that the user might need to access immediately after or while performing text input. For example, if you use a relative layout to place a button bar at the bottom of the screen, using "adjustResize" resizes the layout so the button bar appears above the input method.

此网站会使用 Cookie 来存储您在此网站上指定的语言和显示选项偏好设置。

获取最新的 Android Developers 资讯和提示,助您在 Google Play 上取得成功。

* 必填字段

成功!

在微信上关注 Google Developers

要以浏览此网站吗?

您请求访问的是网页,但是您为此网站设置的语言偏好为

要更改您的语言偏好设置并以浏览此网站吗?如果以后您想要更改语言偏好设置,请使用每个页面底部的语言菜单。

该类需要 或更高的 API 级别

此文档已被隐藏,因为您为该文档选择的 API 级别是 。您可以使用左侧导航栏上方的选择器来更改文档的 API 级别。

要详细了解如何根据您的应用需求指定 API 级别,请参阅支持不同平台版本

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)