1. 程式人生 > >輸入法InputConnection

輸入法InputConnection

cal 特定 track oot sid ado 輸入框 dev set

/**
* The InputConnection interface is the communication channel from an
* [email protected] InputMethod} back to the application that is receiving its
* input. It is used to perform such things as reading text around the
* cursor, committing text to the text box, and sending raw key events
* to the application.
*InputConnection 是介於輸入法和應用程序之間的通信通道。

應用程序接收InputConnection的輸入,InputConnection的作用就是循環從Cursor中讀取text。往輸入框中提交這些text。而且發送關鍵指令給應用程序。
* <p>Applications should never directly implement this interface, but
* instead subclass from [email protected] BaseInputConnection}. This will ensure
* that the application does not break when new methods are added to
* the interface.</p>
*應用程序不應直接實現此接口,而是實現其子類BaseInputConnection。子類會保證應用程序在執行中不會停止執行。假如InputConnection添加了新的方法。


* <h3>Implementing an IME or an editor</h3>
* <p>Text input is the result of the synergy of two essential components:
* an Input Method Engine (IME) and an editor. The IME can be a
* software keyboard, a handwriting interface, an emoji palette, a
* speech-to-text engine, and so on. There are typically several IMEs
* installed on any given Android device. In Android, IMEs extend
* [email protected]

/* */ android.inputmethodservice.InputMethodService}.
* For more information about how to create an IME, see the
* <a href="[email protected]}guide/topics/text/creating-input-method.html">
* Creating an input method</a> guide.
*文字輸入是由兩個要素共同作用的結果。輸入法應用程序和控件。


第一個要素:輸入法應用程序。輸入法程序能夠是軟鍵盤、手寫板、表情符號,語音輸入引擎等等。
android系統默認安裝了幾款輸入法程序在android設備上。在android中,輸入法應用程序繼承自InputMethodService。


* The editor is the component that receives text and displays it.
* Typically, this is an [email protected] android.widget.EditText} instance, but
* some applications may choose to implement their own editor for
* various reasons. This is a large and complicated task, and an
* application that does this needs to make sure the behavior is
* consistent with standard EditText behavior in Android. An editor
* needs to interact with the IME, receiving commands through
* this InputConnection interface, and sending commands through
* [email protected] android.view.inputmethod.InputMethodManager}. An editor
* should start by implementing
* [email protected] android.view.View#onCreateInputConnection(EditorInfo)}
* to return its own input connection.</p>
*第二個要素:編輯器。

編輯器是用於接收輸入法提交的文字並顯示的控件。默認這個控件叫做EditText,可是非常多應用程序基於特定需求也會實現他們自己的控件。實現自己定義控件是一個大並且復雜的工作。
同一時候應用程序還要保證他們實現的控件符合android標準EditText行為規範。
控件須要和輸入法保持溝通,控件能夠通過InputConnection接收輸入法提交的文字,通過InputMethodManager發送指令給輸入法。
控件通過onCreateINputConnection得到自己的InputConnection對象。
* <p>If you are implementing your own IME, you will need to call the
* methods in this interface to interact with the application. Be sure
* to test your IME with a wide range of applications, including
* browsers and rich text editors, as some may have peculiarities you
* need to deal with. Remember your IME may not be the only source of
* changes on the text, and try to be as conservative as possible in
* the data you send and as liberal as possible in the data you
* receive.</p>
*假如你實現了自己的輸入法程序,你須要調用方法和應用程序保持溝通。而且確保你的輸入法程序通過了大量應用程序的測試。比如瀏覽器、往控件中提交大量的數據,由於有些應用程序含有特定的輸入規格。你須要處理特定輸入規則。


記住,你的輸入法程序不是唯一一個改變控件內文字內容的來源,故而對待你提交的text和你接收的數據,盡可能保持慎重。


* <p>If you are implementing your own editor, you will probably need
* to provide your own subclass of [email protected] BaseInputConnection} to
* answer to the commands from IMEs. Please be sure to test your
* editor with as many IMEs as you can as their behavior can vary a
* lot. Also be sure to test with various languages, including CJK
* languages and right-to-left languages like Arabic, as these may
* have different input requirements. When in doubt about the
* behavior you should adopt for a particular call, please mimic the
* default TextView implementation in the latest Android version, and
* if you decide to drift from it, please consider carefully that
* inconsistencies in text edition behavior is almost universally felt
* as a bad thing by users.</p>
*假如你實現了自己的編輯器,你就須要提供你自己實現的BaseInputConnection子類實例。此實例用於和輸入法應用程序保持應答。
確保你的控件通過了大部分輸入法的測試,由於他們的行為變化多端。

同一時候確保在多語言下進行測試,包含CJK或者從右到左的語言,比如arabic。

由於這些語言有非常多不同的輸入要求。


當你對實現的控件的行為存有疑問時。你能夠通話我們。或者模仿近期的android版本號的TextView實現。假如你確定在你的系統中使用它,請考慮清楚控件的文字輸入可能讓用戶體驗非常壞。
* <h3>Cursors, selections and compositions</h3>
* <p>In Android, the cursor and the selection are one and the same
* thing. A "cursor" is just the special case of a zero-sized
* selection. As such, this documentation uses them
* interchangeably. Any method acting "before the cursor" would act
* before the start of the selection if there is one, and any method
* acting "after the cursor" would act after the end of the
* selection.</p>
*在android中。光標和所選內容一件事。光標就是一個0內容的所選內容。因而在android中,他們能夠互換。


不論什麽方法提交在光標前行為就是在提交所選內容之前,同理。在光標之後。就是提交在所選內容之後一樣的行為。


* <p>An editor needs to be able to keep track of a currently
* "composing" region, like the standard edition widgets do. The
* composition is marked in a specific style: see
* [email protected] android.text.Spanned#SPAN_COMPOSING}. IMEs use this to help
* the user keep track of what part of the text they are currently
* focusing on, and interact with the editor using
* [email protected] InputConnection#setComposingText(CharSequence, int)},
* [email protected] InputConnection#setComposingRegion(int, int)} and
* [email protected] InputConnection#finishComposingText()}.
* The composing region and the selection are completely independent
* of each other, and the IME may use them however they see fit.</p>
*/
編輯器控件須要可以保持寫作內容區域的能力,就像標準的編輯小部件一樣。
寫作區域會被應用特定樣式。比如:android.text.Spanned#SPAN_COMPOSING,輸入法使用這樣來幫助用戶保持和當前獲取焦點的文字內容追蹤的能力,通過setComposingText或者finishComposingText等方法。

輸入法InputConnection