一.
1.创建一个新的输入法需要继承android.inputmethodservice.InputMethodService,这个类提供了一个输入法的基本实现,例子可以参考sdk中的SoftKeyboard的代码。
2.输入法跟其他application或service一样会被打包成一个apk,在AndroidManifest.xml,把它声明成一个 Service.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.fastinput"> <application android:label="@string/app_label"> <!-- Declares the input method service --> <service android:name="FastInputIME" android:label="@string/fast_input_label" 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> <!-- Optional activities. A good idea to have some user settings. --> <activity android:name="FastInputIMESettings" android:label="@string/fast_input_settings"> <intent-filter> <action android:name="android.intent.action.MAIN"/> </intent-filter> </activity> </application> </manifest> |
3.输入法的service生命周期如下
二.输入法界面元素
输入法有2个主要的界面元素,InputView与Candidates View。
InputView:是用户输入文字的地方,当输入法被显示的时候会调用InputMethodService.onCreateInputView(),在这个函数里创建和返回你想在输入法窗口中显示的Input View.
Candidates View:是用来提供输入选择,在函数InputMethodService.onCreateCandidatesView()中创建,默认为空。
三.设计不同的输入类型
一个程序的文本框可能有不同的输入类型,比如字符,数字,url,email地址等,当你实现一种输入法的时候你就需要知道不同输入方式的区别,输入法不会自动根据不同的输入类型来切换,所以你的输入法需要支持所有的
输入类型。至于输入数据的验证就交由应用程序去负责。
例如,Android中一个Latin输入法提供的字符与数字输入的界面:
调用InputMethodService.onStartInputView()的时候会传递一个 EditorInfo对象来判断输入类型。
例如使用(EditorInfo.inputType & EditorInfo.TYPE_CLASS_MASK)来判断是属于下面的哪种类型:
TYPE_CLASS_NUMBER
TYPE_CLASS_DATETIME
TYPE_CLASS_PHONE
TYPE_CLASS_TEXT
密码输入:注意不要在你的界面中显示密码,除了提醒用户外也不要把密码保存起来。
四.把输入文本传送给应用程序
1.可以发送一个key event来实现
InputConnection ic = getCurrentInputConnection(); long eventTime = SystemClock.uptimeMillis(); ic.sendKeyEvent(new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, keyEventCode, 0, 0, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE)); ic.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime, KeyEvent.ACTION_UP, keyEventCode, 0, 0, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE)); |
InputMethodService.sendDownUpKeyEvents(keyEventCode); |
建议对于一些输入模式使用第一种方法,因为有些按键可能被过滤。
2.通过编辑输入文本,主要使用以下方法。
getTextBeforeCursor() getTextAfterCursor() deleteSurroundingText() commitText() |
比如,一个以Fell开头的文本,你想把它替换成Hello!
InputConnection ic = getCurrentInputConnection(); ic.deleteSurroundingText(4, 0); ic.commitText("Hello", 1); ic.commitText("!", 1); |
五.联想输入
如果你需要联想输入或者输入过程中动态预测输入的文本,你可以参考如下代码:
InputConnection ic = getCurrentInputConnection(); ic.setComposingText("Composi", 1); ... ic.setComposingText("Composin", 1); ... ic.commitText("Composing ", 1); |
六.拦截硬件按键消息
尽管输入法窗口没有foucs,但是它最先收到硬件的按键消息,如果需要处理这些硬件按键消息,你只需要重写InputMethodService.onKeyDown() 与InputMethodService.onKeyUp(),如果你不想处理某个按键,记得调用super.onKey* 。
七.其他注意点
1.提供一个用户可以直接从当前输入法进行相关输入法设置的方式。
2.提供一个用户可以切换不同输入法的方式。
3.让输入法界面尽快的弹出,资源或者耗时长的操作可以稍后加载。
4.当输入法窗口被隐藏的时候,大块的内存分配最好尽快释放
5.确保输入法能包含最常用的字符。
八.例子
可以参考LatinIME的例子: http://android.git.kernel.org/?p=platform/packages/inputmethods/LatinIME.git;a=tree 或者1.5 SDK也提供了一个 SoftKeyboard 的例子。
Over!