InputMethodService详解

android.inputmethodservice.InputMethodService

InputMethodService provides a standard implementation of an InputMethod, which final implementations can derive from(起源于) and customize(定制). See the base class AbstractInputMethodService and the InputMethod interface for more information on the basics of writing input methods.

In addition to the normal Service lifecycle methods, this class introduces some new specific callbacks that most subclasses will want to make use of:

  • onInitializeInterface() for user-interface initialization, in particular to(尤其) deal with configuration changes while the service is running.
  • onBindInput to find out about(获取有关...的信息) switching to(转到) a new client.
  • onStartInput to deal with an input session starting with the client.
  • onCreateInputView(), onCreateCandidatesView(), and onCreateExtractTextView() for non-demand generation of the UI.
  • onStartInputView(EditorInfo, boolean) to deal with input starting within the input area of the IME.

An input method has significant(重要的) discretion(慎重) in how it goes about(着手)its work: the InputMethodService provides a basic framework for standard UI elements (input view, candidates(候选)view, and running in fullscreen mode), but it is up to(该由...决定;胜任)a particular implementor(实现者、执行者) to decide how to use them. For example, one input method could implement an input area with a keyboard, another could allow the user to draw text, while a third could have no input area (and thus not be visible to the user) but instead listen to audio and perform text to speech conversion(语音转换).

In the implementation provided here, all of these elements are placed together in a single window managed by the InputMethodService. It will execute callbacks as it needs information about them, and provides APIs for programmatic(计划性的)control over them. The layout of these elements is explicitly(明白的;明确的) defined:

  • The soft input view, if available, is placed at the bottom of the screen.
  • The candidates view, if currently shown, is placed above the soft input view.
  • If not running fullscreen, the application is moved or resized to be above these views; if running fullscreen, the window will completely cover the application and its top part will contain the extract text of what is currently being edited by the application.

Soft Input View
Central to(对...主要的)most input methods is the soft input view. This is where most user interaction occurs: pressing on soft keys, drawing characters, or however else your input method wants to generate text. Most implementations will simply(仅仅) have their own view doing all of this work, and return a new instance of it when onCreateInputView() is called. At that point, as long as(只要)the input view is visible, you will see user interaction in that view and can call back on the InputMethodService to interact with the application as appropriate(视情况而定).

There are some situations where you want to decide whether or not your soft input view should be shown to the user. This is done by implementing the onEvaluateInputViewShown() to return true or false based on whether it should be shown in the current environment. If any of your state has changed that may impact this, call updateInputViewShown() to have it re-evaluated. The default implementation always shows the input view unless there is a hard keyboard available, which is the appropriate behavior for most input methods.

Candidates View
Often while the user is generating raw text, an input method wants to provide them with a list of possible interpretations of that text that can be selected for use. This is accomplished with the candidates view, and like the soft input view you implement onCreateCandidatesView() to instantiate your own view implementing your candidates UI.

Management of the candidates view is a little different than the input view, because the candidates view tends to be more transient(短暂的), being shown only when there are possible candidates for the current text being entered by the user. To control whether the candidates view is shown, you use setCandidatesViewShown(boolean). Note that because the candidate view tends to be shown and hidden a lot, it does not impact the application UI in the same way as the soft input view: it will never cause application windows to resize(调整大小), only cause them to be panned(平移)if needed for the user to see the current focus.

Fullscreen Mode
Sometimes your input method UI is too large to integrate with(与...成为一体)the application UI, so you just want to take over(接管)the screen. This is accomplished by switching to full-screen mode, causing the input method window to fill the entire screen and add its own "extracted text" editor showing the user the text that is being typed. Unlike the other UI elements, there is a standard implementation for the extract editor that you should not need to change. The editor is placed at the top of the IME, above the input and candidates views.

Similar to the input view, you control whether the IME is running in fullscreen mode by implementing onEvaluateFullscreenMode() to return true or false based on whether it should be fullscreen in the current environment. If any of your state has changed that may impact this, call updateFullscreenMode() to have it re-evaluated. The default implementation selects fullscreen mode when the screen is in a landscape orientation, which is appropriate behavior for most input methods that have a significant input area.

When in fullscreen mode, you have some special requirements(特殊要求)because the user can not see the application UI. In particular, you should implement onDisplayCompletions(CompletionInfo[]) to show completions generated by your application, typically in your candidates view like you would normally show candidates.

Generating Text
The key part of an IME is of course generating text for the application. This is done through calls to the android.view.inputmethod.InputConnection interface to the application, which can be retrieved from getCurrentInputConnection(). This interface allows you to generate raw key events or, if the target supports it, directly edit in strings of candidates and committed text.

Information about what the target is expected and supports can be found through the android.view.inputmethod.EditorInfo class, which is retrieved with getCurrentInputEditorInfo() method. The most important part of this is EditorInfo.inputType; in particular, if this is EditorInfo.TYPE_NULL, then the target does not support complex edits and you need to only deliver raw key events to it. An input method will also want to look at other values here, to for example detect password mode, auto complete text views, phone number entry, etc.

When the user switches between input targets, you will receive calls to onFinishInput() and onStartInput(EditorInfo, boolean). You can use these to reset and initialize your input state for the current target. For example, you will often want to clear any input state, and update a soft keyboard to be appropriate for the new inputType.

@attr

  • ref android.R.styleable#InputMethodService_imeFullscreenBackground

@attr

  • ref android.R.styleable#InputMethodService_imeExtractEnterAnimation

@attr

  • ref android.R.styleable#InputMethodService_imeExtractExitAnimation
原文地址:https://www.cnblogs.com/fengzhblog/p/2787714.html