AndroidIME之——在代碼中強制切換和獲得當前IME

來源:互聯網
上載者:User

強制轉換輸入法

1.InputMethodManager.setInputMethod (IBinder token, String id)
public void setInputMethod (IBinder token, String id)
Force switch to a new input method component. This can only be called from the currently active input method, as validated by the given token.
Parameters
token
Supplies the identifying token given to an input method when it was started, which allows it to perform this operation on itself.
id The unique identifier for the new input method to be switched to.
此函數,當然是可以實現IME的強制切換的,但是,IBinder的獲得卻需要一定的許可權,調用起來需要比較深入,哥們能力有限專研的一陣子未果。日後,再做深入探討吧。哪位仁兄有解,歡迎留言,不勝感激!!

2.InputMethodService.switchInputMethod(String id)
public void switchInputMethod (String id)
Since: API Level 3 Force switch to a new input method, as identified by id. This input method will be destroyed, and the requested one started on the current input field.
Parameters
id
Unique identifier of the new input method ot start.

相對第一個API來說,這個卻簡單的多。最直觀來看的話,就是這裡不用獲得相應的IBinder!我們幾乎已經勝利了,但是,還是需要點破這層紙的。
首先,關於該API在何處調用?
該API屬於類InputMethodService,而每個輸入應用的入口類又都是繼承於該類的,所以,在IME入口類調用該API就可以了。比如,Google的SDK中給出的Google拼音IME,就在類PinyinIME.java中調用就可以了!
再者,關於參數id?
從定義來看,它是一個String類型,看Google的解釋也未免籠統。經小可仔細專研,原來,是這樣的,還是用例子來說吧,依然選擇上述例子。 Google拼音IME的id就是:com.android.inputmethod.pinyin/.PinyinIME。完整的調用方法就 是:switchInputMethod(“com.android.inputmethod.pinyin/.PinyinIME”);
那麼,如果不在入口類中如何調用呢?
在需要調用的類中定義:private PinyinIME mImeService;
需要的地方調用就可以了:mImeService.switchInputMethod(“com.android.inputmethod.pinyin/.PinyinIME”);

3.Settings.Secure.putString(ContentResolver resolver,String name, String value) (android.provider.Settings)

例如  Settings.Secure.putString(InPutChange.this.getContentResolver(),
                            Settings.Secure.DEFAULT_INPUT_METHOD,"com.android.inputmethod.pinyin/.PinyinIME"); (這裡InPutChange是我自己寫的一個Activity)
這個最簡單  也是最後執行者

實際上當我們調用第二種方法時,它會調用第一種的方法,第一種方法又會調用第三種方法,

獲得當前IME

Settings.Secure.getString(ContentResolver resolver, String name)

例如   String ss=Settings.Secure.getString(InPutChange.this.getContentResolver(),

                Settings.Secure.DEFAULT_INPUT_METHOD);(這裡InPutChange是我自己寫的一個Activity)

這樣可以獲得當前IME種類的id     我當前系統目前IME的結果是"com.android.inputmethod.pinyin/.PinyinIME"     所以ss結果為"com.android.inputmethod.pinyin/.PinyinIME"

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.