Android basic memo (soft keyboard)

Source: Internet
Author: User

1. How the keyboard works
 
The soft keyboard is actually a Dialog. InputMethodService creates a Dialog for our input method and sets some parameters so that they can be displayed at the bottom or in full screen. When you click the input box, the system will adjust the current main window to set aside space for displaying the Dialog at the bottom or full screen.
 
2. Adjust the main activity window
Android defines a property windowSoftInputMode, which allows the program to control the adjustment of the Main Window of the activity. You can set the Activity in the configuration file AndroidManifet. xml. The setting of this attribute will affect two things:
1> keyboard status-hide or display.
2> adjust the main window of the activity-whether to reduce the size of the main window to free up space for the soft keyboard or whether the current focus of its content is visible when the part of the activity window is covered by the soft keyboard.
Therefore, the setting of this attribute must be a value in the following list or a "state ..." Add "adjust ..." Value combination. Set multiple values in any group. Separate values with |.
"StateUnspecified": The state of the soft keyboard (whether it is hidden or visible) is not specified. the system will choose an appropriate state or rely on the setting in the theme. this is the default setting for the behavior of the soft keyboard. the keyboard status (hidden or visible) is not specified. The system selects an appropriate status or topic-dependent setting. This is the default setting of software disk behavior.
"StateUnchanged": The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore. The soft keyboard remains in the last state.
"StateHidden": The soft keyboard is hidden when the user chooses the activity-that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity. when you select this Activity, the keyboard is hidden.
"StateAlwaysHidden": The soft keyboard is always hidden when the activity's main window has input focus. The soft keyboard is always hidden.
"StateVisible": The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's main window). The keyboard is visible.
"StateAlwaysVisible": The soft keyboard is made visible when the user chooses the activity-that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity. when you select this Activity, the keyboard is visible.
"AdjustUnspecified": It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the contents of the window pan to make the currentfocus visible on-screen. the system will automatically select one of these modes depending on whether the content of the window has any layout views that can scroll their contents. if there is such a view, the window will B E resized, on the assumption that scrolling can make all of the window's contents visible within a smaller area. this is the default setting for the behavior of the main window. it is not specified whether the Activity main window is adjusted to set aside space for the soft keyboard, or whether the content of the window is visible to the current focus on the screen. The system automatically selects one of these modes, depending on whether the content of the window has any layout view that can scroll their content. If there is such a view, the window will be adjusted. This assumption can make the content of the scrolling window visible in a small area. This is the default behavior settings for the main window. That is to say, the system automatically determines whether to adopt the translation mode or the compression mode, and determines whether the content can be rolled.
"AdjustResize": (compression mode) The activity's main window is always resized to make room for the soft keyboard on screen. when a keyboard pops up, adjust the screen size of the main window to reserve space for the keyboard.
"AdjustPan": (translation mode: when the input box is not blocked, the layout is not adjusted in this mode. However, when the input box is to be blocked, the window is translated. That is to say, this mode always keeps the input box visible .) The activity's main window is not resized to make room for the soft keyboard. rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. this is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the windo W. The main window of the Activity does not adjust the screen size to allow space on the keyboard. On the contrary, the content of the current window is automatically moved so that the current focus is not overwritten by the keyboard and the user can always see the part of the input content. This is usually not expected to be adjusted because the user may close the keyboard to obtain interaction with the covered content ..
3. Listen to the show and hide of the Soft Keyboard
Sometimes, using the system's own mechanism to adjust the main window is not the expected result. We may want to manually modify the layout when the keyboard is hidden, in order to make the soft keyboard pop-up more beautiful. In this case, you need to listen to the hidden display of the soft keyboard.
We can use the soft keyboard to display and hide the feature, re-layout the main window to listen. If we set the compression mode, we can track the layout of the onSizeChanged function. If it is in the translation mode, this function may not be called.
 
4. The EditText keyboard is not displayed by default.
Method 1:
In AndroidMainfest. xml, select the activity and set the windowSoftInputMode attribute to adjustUnspecified | stateHidden.
Example: <activity android: name = ". Main"
Android: label = "@ string/app_name"
Android: windowSoftInputMode = "adjustUnspecified | stateHidden"
Android: configChanges = "orientation | keyboardHidden">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
Method 2:
Defocus EditText. Use the clearFocus method of EditText.
For example, EditText edit = (EditText) findViewById (R. id. edit );
Edit. clearFocus ();
Method 3:
Force hide Android Input Method window
For example, EditText edit = (EditText) findViewById (R. id. edit );
InputMethodManager imm = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE );
Imm. hideSoftInputFromWindow (edit. getWindowToken (), 0 );
5. EditText always does not bring up the software keyboard
Example: EditText edit = (EditText) findViewById (R. id. edit );

 
Edit. setInputType (InputType. TYPE_NULL );

From heart fly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.