2014-01-23 33 views
3

我需要根據語言的變化改變鍵盤。如何以編程方式更改輸入法?

我已經做了一些研究,發現它可以使用這些API

  1. InputMethodManager setInputMethod(android.os.IBinder,java.lang.String中)
  2. InputMethodService switchInputMethod(完成java.lang.String中)

對於第一API,我需要一個的IBinder令牌其可以是通過調用

mInputMethodService.getWindow()從InputMethodService實例作出。getWindow()。的getAttributes()。令牌

,或者如果我有參考InputMethodService對象我可以簡單地調用

mInputMethodService.switchInputMethod(ID)

改變輸入法。

真正的問題是如何獲得對InputMethodService對象的引用。

PS: 我不想使用InputMethodManager的showInputMethodPicker()因爲我的要求我想從具有語言列表我現有的對話框更改 它。

我知道這對用戶應用程序來說是不可能的,但不知道系統應用程序是否也不可能。

回答

4

成功!

我想通過自定義它來改變當前IME的唯一方法。

對於我的resp。問題如果我從我的自定義設置應用程序中將系統語言 更改爲中文,我必須將鍵盤更改爲中文。

下面討論的方法用於定製LatinIME應用程序。

每個輸入法都有一個擴展InputMethodService類的類。在這個類中,我們可以覆蓋一個名爲onInitializeInterface的方法 。每次配置更改時都會調用此方法,即在更改系統語言環境時將調用 。

在這裏,我們可以檢查當前選擇的語言環境是否受 當前IME支持。如果不是的話,我們可以通過調用方法 switchInputMethod(id)來加載相應的IME。

要獲得ID,我們可以通過inputMethodManager查詢和獲取可用的ID

String pinyinId = ""; 

    InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext() 
        .getSystemService(INPUT_METHOD_SERVICE); 
    List<InputMethodInfo> inputMethodInfos = inputMethodManager.getInputMethodList(); 

    for (InputMethodInfo inputMethodInfo : inputMethodInfos) { 
      if (inputMethodInfo.getId().contains("pinyin")) { 
        pinyinId = inputMethodInfo.getId(); 
      } 
    } 

列表獲取ID,我們可以調用switchInputMethod(pinyinId),它會改變輸入法之後。

+0

謝謝你,這真棒。 –

相關問題