Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android輸入法框的梳理

Android輸入法框的梳理

編輯:Android開發實例

 

/frameworks/base/services/java/InputMethodManagerService.java

這是整個系統當中,一切與輸入法有關的地方的總控制中心。它通過管理下面三個模塊來實現系統的輸入法框架。

1、/frameworks/base/services/java/WindowManagerService

負責顯示輸入法,接收用戶事件。

2、/frameworks/base/core/java/android.inputmethodservice/InputMethodService

輸入法內部邏輯,鍵盤布局,選詞等,最終把選出的字符通過commitText提交出來。要做一個像搜狗輸入法這樣的東西的話,主要就是在這裡做文章。

3、InputManager

由UI控件(View,TextView,EditText等)調用,用來操作輸入法。比如,打開,關閉,切換輸入法等。

下面說一下InputMethodManagerService這個控制中心是怎麼樣與三個模塊交互的。

1、與WindowManagerSerivce的交互。

首先,InputMethodManagerService在初始化時,會調用IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE)),得到IWindowManager這個代理,然後通過IWindowManager與WindowManagerService交互。比如下面這些操作:

調用mIWindowManager.addWindowToken(mCurToken, WindowManager.LayoutParams.TYPE_INPUT_METHOD),讓WindowManagerService顯示輸入法界面。

調用mIWindowManager.removeWindowToken(mCurToken)讓輸入法界面關閉。

調用mIWindowManager.inputMethodClientHasFocus(client)判斷輸入法是否聚焦。

2、與InputMethodService的交互。

InputMethodManagerService在內部維護著一個ArrayList<InputMethodInfo> mMethodList。這個列表會在服務啟動時通過PackageManager查詢當前系統中的輸入法程序來得到。與之對應的,每一個輸入法程序的AndroidManifest.xml中都會有一個Service,而每個Service中都會有標記來告訴系統,自己是個輸入法程序。下面這個是我從系統自帶的例子Samples/SoftKeyboard/AndroidManifest.xml中的取出來的:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

        package="com.example.android.softkeyboard">

    <application android:label="@string/ime_name">

        <service android:name="SoftKeyboard"

                android:permission="android.permission.BIND_INPUT_METHOD">

            <intent-filter>

                <action android:name="android.view.InputMethod" />

            </intent-filter>

            <meta-data android:name="android.view.im" android:resource="@xml/method" />

        </service>

    </application>

</manifest>

另外,InputMethodManagerService內部還有一個PackageReceiver,當系統中有程序的安裝、刪除、重啟等事件發生時,會更新mMethodList。InputMethodManagerService打開,關閉,切換輸入法時,其實就是在操作mMethodList中某個InputMethodInfo。把InputMethodInfo中的代表某個輸入法的InputMethodService啟動或者銷毀,就實現了輸入法的打開和關閉。

3、與InputMethodManager的交互

InputMethodManager中會包含一個IInputMethodManager,這個東西就是InputMethodManagerService的代理,打開關閉輸入法這些操作就是由InputMethodManager中的某些方法調用IInputMethodManager中相應的方法來實現的。比如:

mService.getInputMethodList()獲取輸入法列表。

mService.updateStatusIcon(imeToken, packageName, iconId)更新輸入法圖標,即屏幕上方狀態欄中的輸入法圖標。

mService.finishInput(mClient)隱藏當前輸入法。這所以不說關閉輸入法,是因為輸入法服務啟動起來以後,只有在系統關閉或者切換輸入法時才會關閉。

mService.showSoftInput(mClient, flags, resultReceiver)打開當前輸入法。

...

分別介紹完三大模塊之後,再介紹兩個東西,輸入法的實現和怎麼樣調用輸入法。

1、以系統的SoftKeyboard為例,實現一個輸入法至少需要Keyboard,KeyboardView,CandidateView,SoftKeyboard這四個東西。

CandidateView負責顯示軟鍵盤上面的那個候選區域。

Keyboard負責解析並保存鍵盤布局,並提供選詞算法,供程序運行當中使用。其中鍵盤布局是以XML文件存放在資源當中的。比如我們在漢字輸入法下,按下b、a兩個字母。Keyboard就負責把這兩個字母變成爸、把、巴等顯示在CandidateView上。

KeyboardView負責顯示,就是我們看到的按鍵。

上面這兩人東西合起來,組成了InputView,就是我們看到的軟鍵盤。

SoftKeyboard繼承了InputMethodService,啟動一個輸入法,其實就是啟動一個InputMethodService,當SoftKeyboard輸入法被使用時,啟動就會啟動SoftKeyboard這個Service。InputMethodService中管理著一個繼承自Dialog的SoftInputWindow,而SoftInputWindow裡面就包括了InputView和CandidateView這兩個東西。

2、怎麼樣調用輸入法呢?

說起這個東西,很自然地想起EditText來,我們團隊跟蹤過這個Widget,EditText本身很簡單,主要的代碼在TextView和View當中。這兩個Widget本身又很復雜,雜在一起說不清楚。這裡我就把我們團隊以前做過的一個小例子拿進來做參考,說明一下如何從一個View上調用輸入法和如何接收輸入法傳過來的字符串。

小例子的起源來自於我們要做一個浏覽器,需要在SurfaceView來在Canvas上面繪制自己需要的東西,開啟自己的主控制循環線程,事件處理等。比如我要在SurfaceView上繪制輸入浏覽器中的按鈕、文本、圖片、輸入框等,當然這些和ImageView,TextView沒有關系,都是用自己的UI引擎來做的。最後所有問題都解決了,卻在輸入框上卡殼了。因為要實現輸入,得調用EditText,否則就必須自己去和EditText一樣連接輸入法。以前找過相關資料,看網上也有人碰到過這個問題,但都沒有答案。最後,還是團隊中一個很牛的娃給解決了。代碼很簡單,不出二十行,但沒資料,View的源碼又太龐大,費的勁卻是只有我們團隊的人才能體會得到的。。。這裡佩服張老二同學一下,沒有他的努力,就沒有下面這二十多行很重要很重要的源碼的誕生。

首先,定義一個繼承自BaseInputConnection的類。

public class MyBaseInputConnection extends BaseInputConnection{

public MyBaseInputConnection(View targetView, boolean fullEditor) {

super(targetView, fullEditor);

}

public static String tx="";

@Override

public boolean commitText(CharSequence text, int newCursorPosition) {//輸入法程序就是通過調用這個方法把最終結果輸出來的。

tx = text.toString();

return true;

}

}

BaseInputConnection相當於一個InputMethodService和View之間的一個通道。每當InputMethodService產生一個結果時,都會調用BaseInputConnection的commitText方法,把結果傳遞出來。

public class MyView extends SurfaceView ...{

InputMethodManager input = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);//得到InputMethodManager。

ResultReceiver receiver = new ResultReceiver(new Handler() {//定義事件處理器。

public void handleMessage(Message msg) {

}

});

... ...

input.showSoftInput(this, 0, mRR);//在你想呼出輸入法的時候,調用這一句。

... ...

@Override

public InputConnection onCreateInputConnection(EditorInfo outAttrs) {//這個方法繼承自View。把自定義的BaseInputConnection通道傳遞給InputMethodService。

return new MyBaseInputConnection(this, false);

}

}

低級界面上面,自己調用輸入法並接收輸入法的輸出結果,就是這樣的。

下面我想提一下和這個話題相關的另外一件事,就是前面解決過的一個Bug:

http://blog.csdn.net/a345017062/archive/2011/01/04/6116305.aspx

通過這個問題,可以看出WebView上面的輸入法是如何實現的。簡單來說,WebView就是一個ViewGroup,它裡面有兩層,上層是一個EditText,下層是浏覽器頁面。當浏覽器的輸入框被用戶點中,需要顯示輸入法時,就把上層EditText的位置移到浏覽器的輸入框的位置,高速好EditText的大小和樣式後,讓EditText和浏覽器頁面融為一體,效果就很好了。

通常來說,這個方式應該比自己調用輸入法要好些。可以少做很多事。不過,如果產品經理是個很有想像力的人的話,你就不能滿足他設計出來的有可能極端變態卻非常炫的輸入效果了。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved