Android keyboard display and hide, android keyboard hide

Source: Internet
Author: User

Android keyboard display and hide, android keyboard hide

I. do not automatically pop up the keyboard:

If the EditText control is used, the focus is automatically displayed for the first time, and the keyboard is displayed. If you do not want to automatically display the keyboard, you can use either of the following methods:

Method 1: Set the corresponding activity in the mainfest File

Android: windowSoftInputMode = "stateHidden" or android: windowSoftInputMode = "stateUnchanged ".

 

 

 

Value

Description

"StateUnspecified"

The keyboard status (whether it is hidden or visible) is not specified. The system selects an appropriate status or topic-dependent setting.

This is to set the default behavior of the software disk.

"StateUnchanged"

The keypad remains visible or hidden no matter what the last time it was, when the main window appears in front of it.

"StateHidden"

When the user selects the Activity, the soft keyboard is hidden-that is, when the user is sure to navigate to the Activity, instead of returning it because it leaves another Activity.

"StateAlwaysHidden"

The keyboard is always hidden when the Activity Main Window gets the focus.

"StateVisible"

The soft keyboard is visible when it is appropriate (when you navigate to the Activity main window ).

"StateAlwaysVisible"

When you select this Activity, the soft keyboard is visible-that is, when you are sure to navigate to this Activity, instead of returning it because it leaves another Activity.

"AdjustUnspecified"

It is not specified whether the main window of the Activity is adjusted to set aside the space of 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.

"AdjustResize"

The screen size of the main Activity window is always adjusted to reserve space for the keyboard.

"AdjustPan"

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.

 

In this way, the edit box also has a cursor. How to hide the cursor: EditText. setCursorVisible (false ).

Method 2: You can place a hidden TextView in the layout and requsetFocus during onCreate.

Note that do not set Visiable = gone for TextView; otherwise, it will become invalid. You can place a hidden TextView in the layout and requsetFocus during onCreate.

<TextView

Android: id = "@ + id/text_notuse"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: focusable = "true"

Android: focusableInTouchMode = "true"/>

TextView textView = (TextView) findViewById (R. id. text_notuse );

TextView. requestFocus ();

2. Manually display and hide the system keyboard

1. method 1 (if the input method is displayed in the window, it is hidden, and vice versa ). The usage of this method is unknown.

InputMethodManager imm = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE );

Imm. toggleSoftInput (0, InputMethodManager. HIDE_NOT_ALWAYS );

 

2. method 2 (view is the view that accepts soft keyboard input, SHOW_FORCED indicates forced display)

InputMethodManager imm = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE );

Imm. showSoftInput (view, InputMethodManager. SHOW_FORCED );

This method is called in the onCreate (or onCreateView) function of activity or fragment. It does not work because it is in onCreate or other declared periodic functions (onStart, onResume, etc, the EditText has not been drawn, and InputMethodManager cannot obtain the focus of the control, so the keyboard is not displayed, and the manager. the showSoftInput function returns false, indicating that the keyboard is not displayed. Therefore, the focus can be obtained only when EditText is completely drawn.

Solution:

Edittext. getViewTreeObserver (). addOnGlobalLayoutListener (new ViewTreeObserver. OnGlobalLayoutListener (){
@ Override
Public void onGlobalLayout (){
InputMethodManager manager = (InputMethodManager) getActivity (). getSystemService (getActivity (). INPUT_METHOD_SERVICE );
Manager. showSoftInput (edittext, 0 );
}
});

Note: Do not add the toggleSoftInput method to this listener event. Otherwise, the keyboard will flash continuously.

3. Force hide the keyboard 

Imm. hideSoftInputFromWindow (view. getWindowToken (), 0 );

Note: If the second parameter is HIDE_NOT_ALWAYS, it cannot be hidden when SHOW_FORCED is the second parameter of showSoftInput.

4. Call to hide the default system input method

(InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE). Invoke (WidgetSearchActivity. this. getCurrentFocus (). getWindowToken (), InputMethodManager. HIDE_NOT_ALWAYS); (getwidsearchactivity is the current Activity)

5. obtain the status of the input method.

InputMethodManager imm = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE );

Boolean isOpen = imm. isActive (); // If isOpen returns true, the input method is enabled.

6. constants of InputMethodManager:

Public static final intHIDE_IMPLICIT_ONLY

 

hideSoftInputFromWindow(IBinder, int)Indicates that if the user does not explicitly display the keyboard window, the window is hidden.

 

Constant Value: 1 (0x00000001)

 

 

 

Public static final intHIDE_NOT_ALWAYS

 

    hideSoftInputFromWindow(IBinder, int)Indicates that the keyboard window is always hidden, unlessSHOW_FORCEDDisplay.

 

Constant Value: 2 (0x00000002)

 

 

 

Public static final intRESULT_HIDDEN

 

 

 

    showSoftInput(View, int, ResultReceiver)AndhideSoftInputFromWindow(IBinder, int, ResultReceiver)MediumResultReceiverResult code flag: the status of the keyboard window when the display is switched to hidden.

 

Constant Value: 3 (0x00000003)

 

 

 

Public static final intRESULT_SHOWN

 

 

 

    showSoftInput(View, int, ResultReceiver)AndhideSoftInputFromWindow(IBinder, int, ResultReceiver)MediumResultReceiverResult code flag: The status when the keyboard window is changed from hidden to display.

 

Constant Value: 2 (0x00000002)

 

 

 

Public static final intRESULT_UNCHANGED_HIDDEN

 

 

 

    showSoftInput(View, int, ResultReceiver)AndhideSoftInputFromWindow(IBinder, int, ResultReceiver)MediumResultReceiverResult code flag: The status when the keyboard window remains hidden.

 

Constant Value: 1 (0x00000001)

 

 

 

Public static final intRESULT_UNCHANGED_SHOWN

 

 

 

    showSoftInput(View, int, ResultReceiver)AndhideSoftInputFromWindow(IBinder, int, ResultReceiver)MediumResultReceiverResult code flag: The status when the keyboard and keyboard windows remain unchanged.

 

Constant Value: 0 (0x00000000)

 

 

 

Public static final intSHOW_FORCED

 

    showSoftInput(View, int)Sign, indicating that the user forces to open the input method (such as long press the menu key), always open until only explicitly closed.

 

Constant Value: 2 (0x00000002)

 

 

 

Public static final intSHOW_IMPLICIT

 

showSoftInput(View, int)Indicates that the input window is displayed implicitly, which is not directly required by the user. The window may not be displayed.

Constant Value: 1 (0x00000001)

Iv. Public Methods

Public voidDisplayCompletions(View view, CompletionInfo [] completions)

(Note: The input method is automatically completed)

 

Public InputMethodSubtypeGetCurrentInputMethodSubtype()

(Translator's note: Get the current input method type ?)

 

Public List <InputMethodInfo>GetEnabledInputMethodList()

(Translator's note: get the list of input methods enabled ?)

 

Public List <InputMethodSubtype> getEnabledInputMethodSubtypeList (InputMethodInfo imi, boolean allowsImplicitlySelectedSubtypes)

 

Public List <InputMethodInfo>GetInputMethodList()

(Translator's note: Get the input method List)

 

Public Map <InputMethodInfo, List <InputMethodSubtype>GetShortcutInputMethodsAndSubtypes()

 

Public voidHideSoftInputFromInputMethod(IBinder token, int flags)

Disable/hide the keyboard area of the input method, and the user no longer sees or interacts with it. It can only be called by the currently activated input method because token verification is required.

Parameters

Tokens are provided for token verification when the input method is started. You can perform operations on tokens after verification.

Flags provides additional operation flags. It can be 0 orHIDE_IMPLICIT_ONLY,HIDE_NOT_ALWAYSSet the equal position.

 

Public booleanHideSoftInputFromWindow(IBinder windowToken, int flags)

hideSoftInputFromWindow(IBinder, int, ResultReceiver)To determine the current input receiving window from the context of the window and hide its input method window.

Parameters

WindowToken requested by windowView.getWindowToken()Returns the token ).

Flags provides additional operation flags. It can be 0 orHIDE_IMPLICIT_ONLYBit settings.

 

Public booleanHideSoftInputFromWindow(IBinder windowToken, int flags, ResultReceiver resultReceiver)

Determine the current input receiving window from the context of the window, and hide its keypad window. It can be called and obtained by the user, not just explicitly requiring that the input method window be hidden.

Parameters

WindowToken requested by windowView.getWindowToken()Returns the token ).

Flags provides additional operation flags. It can be 0 orHIDE_IMPLICIT_ONLYBit settings.

If resultReceiver is not empty, it is called when the IME process the request and tells you that it is complete. The result code you receive can beRESULT_UNCHANGED_SHOWN,RESULT_UNCHANGED_HIDDEN,RESULT_SHOWN, OrRESULT_HIDDEN.

 

Public voidHideStatusIcon(IBinder imeToken)

(Note: Hide the status bar icon ?)

 

Public booleanIsAcceptingText()

The current service view is edited in full text and returns true. If no input method is connected to false, the system can only process the original key events.

 

Public booleanIsActive(View view)

The view returns true if it is the active view currently entered.

 

Public booleanIsActive()

Returns true when any view in the input method is activated.

 

Public booleanIsFullscreenMode()

Determine whether the input method runs in full screen mode. In full screen mode, if your UI is completely overwritten, the system returns true. Otherwise, the system returns false.

 

Public booleanIsWatchingCursor(View view)

For example, if you want to see the input editor's cursor position in the current input method, true is returned.

 

Public voidRestartInput(View view)

If any input method is connected to the view, restart the input to display the new content. You can call this method in the following situations: the text of the view causes the appearance of the input method to change or there is a key input stream, such as when the application calls TextView. setText.

Parameters

View text changes.

 

Public voidSendAppPrivateCommand(View view, String action, Bundle data)

Call the current Input MethodInputMethodSession.appPrivateCommand().

Parameters

View can be an optional view for sending commands. If you want to send commands without considering attaching the view to the input method, this option can be empty.

The name of the Command executed by the action. It must be the name of the scope, such as the prefix package name, so that different developers will not create conflicting commands.

Any data contained in the data command.

 

Public booleanSetCurrentInputMethodSubtype(InputMethodSubtype subtype)

(Note: This method is newly added in 3.0)

 

Public voidSetInputMethod(IBinder token, String id)

Force switch to the new input method component. The currently activated input method can only be called by the application or service that holds the token.

Parameters

Tokens are provided for token verification when the input method is started. You can perform operations on tokens after verification.

Id to the unique identifier of the new input method.

 

Public voidSetInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype)

Force switch to a new input method and the specified type. The currently activated input method can only be called by the application or service that holds the token. (Note: This method is newly added in 3.0)

Parameters

Tokens are provided for token verification when the input method is started. You can perform operations on tokens after verification.

Id to the unique identifier of the new input method.

Switch subtype to the new input type.

 

Public voidShowInputMethodAndSubtypeEnabler(String topId)

(Note: This method is newly added in 3.0)

 

Public voidShowInputMethodPicker()

(Translator's note: display the input method menu list)

 

Public booleanShowSoftInput(View view, int flags, ResultReceiver resultReceiver)

If necessary, the keyboard area of the current input method must be explicitly displayed to the user. This method can be called when the user interacts with the view and indicates that the input operation is to begin.

Parameters

View Current focus view, which can be input by the keyboard.

Flags provides additional operation flags. It can be 0 orSHOW_IMPLICITBit settings.

If resultReceiver is not empty, it is called when the IME process the request and tells you that it is complete. The result code you receive can beRESULT_UNCHANGED_SHOWN,RESULT_UNCHANGED_HIDDEN,RESULT_SHOWN, OrRESULT_HIDDEN.

 

Public booleanShowSoftInput(View view, int flags)

showSoftInput(View, int, ResultReceiver)If needed, the keyboard area of the current input method must be explicitly displayed to the user.

Parameters

View Current focus view, which can be input by the keyboard.

Flags provides additional operation flags. It can be 0 orSHOW_IMPLICITBit settings.

 

Public voidShowSoftInputFromInputMethod(IBinder token, int flags)

Displays the soft keyboard area of the input method, so that you can see and interact with the input method window. It can only be called by the currently activated input method because token verification is required.

Parameters

Tokens are provided for token verification when the input method is started. You can perform operations on tokens after verification.

Flags provides additional operation flags. It can be 0 orSHOW_IMPLICIT,SHOW_FORCEDBit settings.

 

Public voidShowStatusIcon(IBinder imeToken, String packageName, int iconId)

(Translator's note: displays the status bar icon ?)

 

Public booleanSwitchToLastInputMethod(IBinder imeToken)

 

Public voidToggleSoftInput(Int showFlags, int hideFlags)

(Translator's note: Switching the keyboard)

 

Public voidToggleSoftInputFromWindow(IBinder windowToken, int showFlags, int hideFlags)

In this method, the input method window is displayed. If the input window is displayed, it is hidden. If no input window is displayed.

Parameters

WindowToken requested by windowView.getWindowToken()Returns the token ).

ShowFlags provides additional operation flags. It can be 0 orHIDE_IMPLICIT_ONLYBit settings.

HideFlags provides additional operation flags. It can be 0 orHIDE_IMPLICIT_ONLY,HIDE_NOT_ALWAYSBit settings.

 

Public voidUpdateCursor(View view, int left, int top, int right, int bottom)

Returns the current cursor position of the window.

 

Public voidUpdateExtractedText(View view, int token, ExtractedText text)

(Note: When the content changes, the text editor calls this method to notify the new extracted text .)

 

Public voidUpdateSelection(View view, int selStart, int selEnd, int candidatesStart, int candidatesEnd)

Return to the selected region. Http://blog.csdn.net/h7870181/article/details/8332991 http://blog.csdn.net/pi9nc/article/details/9196779 Co., http://blog.csdn.net/lxlmycsdnfree/article/details/60962731.

 


Related Article

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.