Introduction to Android Inputmethodmanager Input Method _android

Source: Internet
Author: User
Tags bit set switches

First, the structure

Public final class Inputmethodmanager extends Object

Java.lang.Object

Android.view.inputmethod.InputMethodManager

Ii. Overview of the class

The core API for the entire input framework (IMF) structure, scheduling between applications and the current input method interaction. You can use Context.getsystemservice () to get an example of this interface.

General Statement of Architecture (architecture Overview)

The input frame (IMF) has three main parts:

* Input Method Manager, management of the various parts of the interaction. It is a client API that exists in the context of each application to communicate the global system services that govern the interaction between all processes.

* Input Method (IME) to implement a standalone interactive module that allows the user to generate text. The system binds a current input method. Make it create and build to determine when the input method hides or displays its UI. Only one IME can be run at the same time.

* Client applications that control input focus and IME status through the Input method manager. Only one client can use the IME at a time.

Application (applications)

In most cases, applications that use standard TextView or their subclasses can make the soft keyboard (soft input methods) work as long as they do a small amount of work. What you should be aware of is:

* Correctly set your editable text view InputType so that the input method has enough context to help the user enter text into it.

* When the input method is displayed, properly handle the hidden screen space. Ideally the application should handle the window to be smaller, and if necessary you can use the system to perform the panning window. You should set the Windowsoftinputmode property or adjust the corresponding value of the Create window in the activity to allow the system to decide to pan or zoom (it will try to adjust automatically but it may be wrong).

* You can also use the same Windowsoftinputmode control window's preferred soft keyboard status (open, close, etc.).

The API enables more granular control over the IMF and its IME, such as display or input areas, user selection of an input method, and so on.

When you write your own text editor, you implement Oncreateinputconnection (Editorinfo) to return to your Inputconnection interface instance to allow the IME to interact with your text-editing field.

Import method (Input Methods)

An Input Method Editor (IME) is an implementation of a service (services) that is usually inherited from Inputmethodservice. The IME provides the core Inputmethod interface, although providing inputmethod is usually handled by Inputmethodservice, and the implementation of the IME only needs to handle higher-level APIs.

See Inputmethodservice for more information.

Safety (Security)

Input methods are related to a number of security issues because they are basically unconstrained to drive the UI and monitor user input. The Android input framework also allows the scheduling of Third-party IMEs, which must be careful to limit their choice and interaction.

Here are the key elements behind the IMF's security architecture:

* Only allow the system to access the Inputmethod interface with Bind_input_method permission to access the IME. This is enforced by binding to a service that requires this permission. So the system can guarantee that no untrusted client accesses the current input method outside of its control.

* There may be many client processes in the IMF, but at the same time only one is active. Inactive clients cannot be implemented with IMF core interactions through the following mechanisms.

* The Input method client can only access the Inputmethodsession interface. Each client creates an interface instance, and only calls to the session associated with the activation client are processed by the IME. This is done through the abstractinputmethodservice of the ordinary IME, which must be explicitly handled by the IME, which is the custom implementation of the Inputmethodsession.

* Only active inputconnection accept the operation. The IMF informs each client whether the process is active, and the IMF ignores calls to the current input join by the inactive process. This ensures that the current IME only delivers events and text edits to the UI that the user is visible to focus on.

* An IME can never interact with inputconnection when the screen is closed. This is done by invalidating all clients when the screen closes, and prevents the unhealthy IME from driving the UI when users are unable to focus on their behavior.

* The client application can require the system to select a new IME, but not one of the programmatic choices. This is to prevent a malicious program from selecting its own IME and keeping it running while the user navigates to another application. That is, an IME can programmatically require the system to select a different IME because it has full control over user input.

Users must explicitly enable it before they can switch to a new IME to confirm that the system understands it and makes it available.

Third, constant

public static final int hide_implicit_only

A flag in Hidesoftinputfromwindow (ibinder, int) that indicates that a window is hidden if the user does not explicitly display a soft keyboard window.

Constant value: 1 (0x00000001)

public static final int hide_not_always

A flag in Hidesoftinputfromwindow (ibinder, int) that indicates that the Soft keyboard window is always hidden, unless it is initially displayed in show_forced.

Constant value: 2 (0x00000002)

public static final int Result_hidden

Showsoftinput (View, int, resultreceiver) and Hidesoftinputfromwindow (ibinder, int, resultreceiver) Resultreceiver result Code flag: the state in which the Soft keyboard window switches from display to hidden.

Constant value: 3 (0x00000003)

public static final int Result_shown

Showsoftinput (View, int, resultreceiver) and Hidesoftinputfromwindow (ibinder, int, resultreceiver) Resultreceiver result Code flag: the state at which the Soft keyboard window switches from hidden to display.

Constant value: 2 (0x00000002)

public static final int Result_unchanged_hidden

Showsoftinput (View, int, resultreceiver) and Hidesoftinputfromwindow (ibinder, int, resultreceiver) Resultreceiver result Code flag: The state of the Soft keyboard window unchanged when it remains hidden.

Constant value: 1 (0x00000001)

public static final int Result_unchanged_shown

Showsoftinput (View, int, resultreceiver) and Hidesoftinputfromwindow (ibinder, int, resultreceiver) Resultreceiver result code flag: The soft keyboard window stays the same when it is displayed.

Constant value: 0 (0x00000000)

public static final int show_forced

Showsoftinput (View, int) flag, which means that the user forces the input method (such as a long press Menu key) to open until it is explicitly closed.

Constant value: 2 (0x00000002)

public static final int show_implicit

The Showsoftinput (View, int) flag, which represents an implicit display input window, is not directly required by the user. The window may not be displayed.

Constant value: 1 (0x00000001)

Iv. Public methods

public void displaycompletions (view view, completioninfo[] completions)

(Translator note: Automatic completion of input method)

Public Inputmethodsubtype Getcurrentinputmethodsubtype ()

(Translator Note: Get current Input method type?) )

Public list<inputmethodinfo> getenabledinputmethodlist ()

(Translator Note: Get list of Enabled input methods?) )

Public list<inputmethodsubtype> getenabledinputmethodsubtypelist (Inputmethodinfo IMI, Boolean Allowsimplicitlyselectedsubtypes)

Public list<inputmethodinfo> getinputmethodlist ()

(Translator Note: get input list)

Public Map<inputmethodinfo, list<inputmethodsubtype>> getshortcutinputmethodsandsubtypes ()

public void Hidesoftinputfrominputmethod (IBinder token, int flags)

Closes/hides the Input method soft keyboard area where the user no longer sees or interacts with it. Can only be invoked by the currently active input method because of token (token) validation.

Parameters

Token provides token authentication when the input method is started and can be manipulated after validation.

Flags provide additional operational markings. The current can be 0 or hide_implicit_only, hide_not_always and equal-bit settings.

public boolean Hidesoftinputfromwindow (IBinder windowtoken, int flags)

Hidesoftinputfromwindow (ibinder, int, resultreceiver) without return value: Determines the window from the window context to which the input is currently received, hides its Input method window

Parameters

Windowtoken a token (token) returned by the window request View.getwindowtoken ().

Flags provide additional operational markings. You can now set the 0 or hide_implicit_only bit.

public boolean Hidesoftinputfromwindow (IBinder windowtoken, int flags, Resultreceiver resultreceiver)

Determines which window is currently receiving input from the context of the window, and requires that its soft keyboard window be hidden. It can be invoked by the user and obtained results rather than explicitly requiring an input method window to be hidden.

Parameters

Windowtoken a token (token) returned by the window request View.getwindowtoken ().

Flags provide additional operational markings. You can now set the 0 or hide_implicit_only bit.

Resultreceiver is not NULL when the IME processing request tells you to complete the call. The result code you receive can be Result_unchanged_shown, Result_unchanged_hidden, Result_shown, or Result_hidden.

public void Hidestatusicon (IBinder imetoken)

(Translator Note: Hide the status bar icon?) )

public boolean Isacceptingtext ()

The current service view accepts Full-text edits to return true. No input method join is false, and it can only handle raw key events.

public Boolean isactive (view view)

The view returns True when the active view is currently entered.

public boolean isactive ()

Returns True when any view in the input method is active.

public boolean Isfullscreenmode ()

Determine if the relevant input method is running in Full-screen mode. When full screen, completely overwrite your UI, return True, otherwise return false.

public Boolean iswatchingcursor (view view)

Returns true if the current input method wants to see the input Editor's cursor position.

public void Restartinput (view view)

If an input method is joined to the view, restart the input to display the new content. This method can be called when the text of a view causes an input method to change its appearance or has a key input stream, such as when an application invokes Textview.settext ().

Parameters

View text that has changed.

public void Sendappprivatecommand (view view, String action, Bundle data)

Call Inputmethodsession.appprivatecommand () on the current input method.

Parameters

View optional views that send commands, such as you want to send a command without regard to the view attached to the IME, this item can be empty.

The name of the command executed by the action. Must be the name of the scope, such as the prefix package name, so that different developers do not create conflicting commands.

Any data that is contained in the data command.

public boolean Setcurrentinputmethodsubtype (Inputmethodsubtype subtype)

(This method is the new method in 3.0)

public void Setinputmethod (IBinder token, String ID)

Forces a switch to the new input method part. The current activation input method can only be invoked by an application (application) or service (services) that holds token.

Parameters

Token provides token authentication when the input method is started and can be manipulated after validation.

ID to switch to the unique identification of the new input method.
public void Setinputmethodandsubtype (IBinder token, String ID, Inputmethodsubtype subtype)

Forces a switch to a new input method and a specified type. The current activation input method can only be invoked by an application (application) or service (services) that holds token. (This method is the new method in 3.0)

Parameters

Token provides token authentication when the input method is started and can be manipulated after validation.

ID to switch to the unique identification of the new input method.

Subtype switch to a new type of input method.

public void Showinputmethodandsubtypeenabler (String topid)

(This method is the new method in 3.0)

public void Showinputmethodpicker ()

(Translator Note: Display input menu list)

public Boolean showsoftinput (view view, int flags, Resultreceiver resultreceiver)

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

Parameters

View is currently focused and can accept soft keyboard input.

Flags provide additional operational markings. The current can be a 0 or show_implicit bit setting.

Resultreceiver is not NULL when the IME processing request tells you to complete the call. The result code you receive can be Result_unchanged_shown, Result_unchanged_hidden, Result_shown, or Result_hidden.

public Boolean showsoftinput (view view, int flags)

Showsoftinput (View, int, resultreceiver) without return value: If necessary, explicitly require the soft keyboard area of the current input method to be displayed to the user.

Parameters

View is currently focused and can accept soft keyboard input.

Flags provide additional operational markings. The current can be a 0 or show_implicit bit setting.

public void Showsoftinputfrominputmethod (IBinder token, int flags)

Displays the soft keyboard area of the input method so that the user can see the Input Method window and interact with it. Can only be invoked by the currently active input method because of token (token) validation.

Parameters

Token provides token authentication when the input method is started and can be manipulated after validation.

Flags provide additional operational markings. can be 0 or show_implicit, show_forced bit set.

public void Showstatusicon (IBinder imetoken, String packagename, int iconid)

(Note: Display status bar icon?) )

public boolean Switchtolastinputmethod (IBinder imetoken)

public void Togglesoftinput (int showflags, int hideflags)

(Translator Note: Toggle soft keyboard)

public void Togglesoftinputfromwindow (IBinder windowtoken, int showflags, int hideflags)

This method switches the input method to the window display. If the input window has been displayed, it is hidden. If no input window is displayed.

Parameters

Windowtoken a token (token) returned by the window request View.getwindowtoken ().

The SHOWFLAGS provides additional operational markings. You can now set the 0 or hide_implicit_only bit.

The HIDEFLAGS provides additional operational markings. can be 0 or hide_implicit_only, hide_not_always bit set.

public void Updatecursor (view view, int left, int. top, int right, int bottom)

Returns the current cursor position of the window.

public void Updateextractedtext (view view, int token, extractedtext text)

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

public void updateselection (view view, int selStart, int selend, int candidatesstart, int candidatesend)

Returns the currently selected range.

How Android invokes the default input method for displaying and hiding systems

1. Invoke the display system default Input method

Method One,

Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);

Imm.showsoftinput (M_receiverview (view) that accepts soft keyboard input), inputmethodmanager.show_forced (provides the tag for the current operation, show_forced indicates a force display) );

Method Two,

Inputmethodmanager m= (Inputmethodmanager) Getsystemservice (Context.input_method_service);
M.togglesoftinput (0, inputmethodmanager.hide_not_always); (This method can implement the input method to switch the display on the window, if the input method is already displayed on the window, hide, if hidden, display input method to the window)

2. Invoke the hidden system default Input method

((Inputmethodmanager) Getsystemservice (Input_method_service)). Hidesoftinputfromwindow ( WidgetSearchActivity.this.getCurrentFocus (). Getwindowtoken (), inputmethodmanager.hide_not_always); (Widgetsearchactivity is the current activity)

3. Get the status of IME open

Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);
Boolean isopen=imm.isactive ();
IsOpen returns True, means the input method opens

1.//Hide Soft keyboard

((Inputmethodmanager) Getsystemservice (Input_method_service)). Hidesoftinputfromwindow ( WidgetSearchActivity.this.getCurrentFocus (). Getwindowtoken (), inputmethodmanager.hide_not_always);

2,//display soft keyboard, control ID can be Edittext,textview

((Inputmethodmanager) Getsystemservice (Input_method_service)). Showsoftinput (Control ID, 0);

3, does not automatically eject the keyboard:

With the EditText control, the focus is automatically received the first time it is displayed, and the keyboard pops up, and if you do not want to automatically eject the keyboard, there are two ways:

Method One: In the Mainfest file, set the corresponding activity

Android:windowsoftinputmode= "Statehidden" or android:windowsoftinputmode= "stateunchanged".

Method Two: Can put a hidden textview in the layout, then Requsetfocus when OnCreate.

Note TextView do not set visiable=gone, otherwise it will fail

, you can put a hidden textview in the layout, and then Requsetfocus in the OnCreate.

Note TextView do not set visiable=gone, otherwise it will fail

<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 ();

Android Soft Keyboard Learning Inputmethodmanager

When we click in the EditText provided by Android, we will automatically eject the soft keyboard, in fact, for the control of the soft keyboard we can inputmethodmanager this class to achieve. The way we need to control the soft keyboard is to have a soft keyboard like edittext when the OnClick event occurs, and to automatically eject the soft keyboard when a program is opened.

public class Inputmethodmanagertest extends activity implements onclicklistener{private button button; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Super.oncre 
    Ate (savedinstancestate); 
    LinearLayout layout=new LinearLayout (this); Linearlayout.layoutparams layoutparams=new linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT); 
    Button=new Button (this); 
    Button.setid (123); 
    Button.settext ("Hello Gaomatrix"); 
    Button.setonclicklistener (this); 
    Layout.addview (button, layoutparams); 
     
    Setcontentview (layout); 
    /** * with a Timer control when the activity is opened when the soft keyboard/Timer timer=new timer (); Timer.schedule (New TimerTask () {@Override public void run () {Inputmethodmanager inputmethodmanage 
        R= (Inputmethodmanager) Getsystemservice (Context.input_method_service); Inputmethodmanager.togglesoftinPut (0, inputmethodmanager.hide_not_always); 
  }, 2000); /** * When the Click event Triggers the display soft keyboard/@Override public void OnClick (View v) {Inputmethodmanager Inputmethodman 
    Ager= (Inputmethodmanager) Getsystemservice (Context.input_method_service); 
  Inputmethodmanager.togglesoftinput (0, inputmethodmanager.hide_not_always); 
 } 
}

The explanation in the API of the Togglesoftinput method in this Inputmethodmanager class is:

This is toggles the Input Method window display. If the input window is already displayed, it gets hidden. If not the input window would be displayed.

This method in the interface to switch the function of input method, if the input method out of the realistic state, it will hide, if in a hidden state, display input method.

And for the second way into the activity will automatically display the soft keyboard, in a timer, that is, in a thread execution, but is delayed 2 seconds of execution, because in the OnCreate function, the Android program does not draw the screen complete.

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.