Android之输入法开发简单说明
2012-03-12 10:06
459 查看
http://news.wangmeng.cn/detailNews/2616-android-development-of-a-simple-description-of-the-input
创建一个输入法,必须继承android.inputmethodservice.InputMethodService,它作为一个服务,监听所有EditText的事件。
看一个AndroidManifest.xml文件的示例:
整个输入法的生命周期如下图所示:
![](http://news.wangmeng.cn/attachments/201008/1282200368475.png)
Input View
软键盘的主界面,在InputMethodService.onCreateInputView() 初始化.
Candidates View
This is where potential word corrections or completions are presented to the user for selection. Again,this may or may not be relevant to your input method and you can return null from calls to InputMethodService.onCreateCandidatesView() ,which is the default
behavior.
![](http://news.wangmeng.cn/attachments/201008/1282200368496.png)
![](http://news.wangmeng.cn/attachments/201008/1282200368509.png)
InputMethodService.onStartInputView() 输入法开始函数。
(EditorInfo.inputType & EditorInfo.TYPE_CLASS_MASK ) can be one of many different values,including:
TYPE_CLASS_NUMBER
TYPE_CLASS_DATETIME
TYPE_CLASS_PHONE
TYPE_CLASS_TEXT
See android.text.InputType for more details.
EditorInfo.inputType can contain other masked bits that indicate the class variation and other flags. For example,TYPE_TEXT_VARIATION_PASSWORD or TYPE_TEXT_VARIATION_URI or TYPE_TEXT_FLAG_AUTO_COMPLETE .
Password fields
Pay specific attention when sending text to password fields. Make sure that the password is not visible within your UI - in neither the input view nor the candidates view. And do not save the password anywhere without explicitly informing the user.
Landscape vs. portrait
The UI needs to be able to scale between landscape and portrait orientations. In non-fullscreen IME mode,leave sufficient space for the application to show the text field and any associated context. Preferably,no more than half the screen should be occupied
by the IME. In fullscreen IME mode this is not an issue.
Sending text to the application
There are two ways to send text to the application. You can either send individual key events or you can edit the text around the cursor in the application's text field.
To send a key event,you can simply construct KeyEvent objects and call InputConnection.sendKeyEvent(). Here are some examples:
Or use the convenience method:
Note : It is recommended to use the above method for certain fields such as phone number fields
because of filters that may be applied to the text after each key press. Return key and delete key should also be sent as raw key events for certain input types,as applications may be watching for specific key events in order to perform an action.
When editing text in a text field,some of the more useful methods on android.view.inputmethod.InputConnection are:
getTextBeforeCursor()
getTextAfterCursor()
deleteSurroundingText()
commitText()
For example,let's say the text "Fell" is to the left of the cursor. And you want to replace it with "Hello!":
Composing text before committing
If your input method does some kind of text prediction or requires multiple steps to compose a word or glyph,you can show the progress in the text field until the user commits the word and then you can replace the partial composition with the completed text.
The text that is being composed will be highlighted in the text field in some fashion,such as an underline.
![](http://news.wangmeng.cn/attachments/201008/1282200368522.png)
Intercepting hard key events
Even though the input method window doesn't have explicit focus,it receives hard key events first and can choose to consume them or forward them along to the application. For instance,you may want to consume the directional keys to navigate within your UI
for candidate selection during composition. Or you may want to trap the back key to dismiss any popups originating from the input method window. To intercept hard keys,override InputMethodService.onKeyDown() and InputMethodService.onKeyUp(). Remember to call
super.onKey * if you don't want to consume a certain key yourself.
Other considerations
Provide a way for the user to easily bring up any associated settings directly from the input method UI
Provide a way for the user to switch to a different input method (multiple input methods may be installed) directly from the input method UI.
Bring up the UI quickly - preload or lazy-load any large resources so that the user sees the input method quickly on tapping on a text field. And cache any resources and views for subsequent invocations of the input method.
On the flip side,any large memory allocations should be released soon after the input method window is hidden so that applications can have sufficient memory to run. Consider using a delayed message to release resources if the input method is in a hidden state
for a few seconds.
Make sure that most common characters can be entered using the input method,as users may use punctuation in passwords or user names and they shouldn't be stuck in a situation where they can't enter a certain character in order to gain access into a password-locked
device.
Samples
For a real world example,with support for multiple input types and text prediction,see LatinIME source code . The Android 1.5 SDK also includes a SoftKeyboard sample as well
创建一个输入法,必须继承android.inputmethodservice.InputMethodService,它作为一个服务,监听所有EditText的事件。
看一个AndroidManifest.xml文件的示例:
01.
<
manifest
xmlns:android
=
"http://schemas.android.com/apk/res/android"
02.
package
=
"com.example.fastinput"
>
03.
04.
<
application
android:label
=
"@string/app_label"
>
05.
06.
<!-- Declares the input method service -->
07.
<
service
android:name
=
"FastInputIME"
08.
android:label
=
"@string/fast_input_label"
09.
android:permission
=
"android.permission.BIND_INPUT_METHOD"
>
10.
<
intent-filter
>
11.
<
action
android:name
=
"android.view.InputMethod"
/>
12.
</
intent-filter
>
13.
<
meta-data
android:name
=
"android.view.im"
android:resource
=
"@xml/method"
/>
14.
</
service
>
15.
16.
<!-- Optional activities. A good idea to have some user settings. -->
17.
<
activity
android:name
=
"FastInputIMESettings"
android:label
=
"@string/fast_input_settings"
>
18.
<
intent-filter
>
19.
<
action
android:name
=
"android.intent.action.MAIN"
/>
20.
</
intent-filter
>
21.
</
activity
>
22.
</
application
>
23.
</
manifest
>
整个输入法的生命周期如下图所示:
![](http://news.wangmeng.cn/attachments/201008/1282200368475.png)
Input View
软键盘的主界面,在InputMethodService.onCreateInputView() 初始化.
Candidates View
This is where potential word corrections or completions are presented to the user for selection. Again,this may or may not be relevant to your input method and you can return null from calls to InputMethodService.onCreateCandidatesView() ,which is the default
behavior.
![](http://news.wangmeng.cn/attachments/201008/1282200368496.png)
![](http://news.wangmeng.cn/attachments/201008/1282200368509.png)
InputMethodService.onStartInputView() 输入法开始函数。
(EditorInfo.inputType & EditorInfo.TYPE_CLASS_MASK ) can be one of many different values,including:
TYPE_CLASS_NUMBER
TYPE_CLASS_DATETIME
TYPE_CLASS_PHONE
TYPE_CLASS_TEXT
See android.text.InputType for more details.
EditorInfo.inputType can contain other masked bits that indicate the class variation and other flags. For example,TYPE_TEXT_VARIATION_PASSWORD or TYPE_TEXT_VARIATION_URI or TYPE_TEXT_FLAG_AUTO_COMPLETE .
Password fields
Pay specific attention when sending text to password fields. Make sure that the password is not visible within your UI - in neither the input view nor the candidates view. And do not save the password anywhere without explicitly informing the user.
Landscape vs. portrait
The UI needs to be able to scale between landscape and portrait orientations. In non-fullscreen IME mode,leave sufficient space for the application to show the text field and any associated context. Preferably,no more than half the screen should be occupied
by the IME. In fullscreen IME mode this is not an issue.
Sending text to the application
There are two ways to send text to the application. You can either send individual key events or you can edit the text around the cursor in the application's text field.
To send a key event,you can simply construct KeyEvent objects and call InputConnection.sendKeyEvent(). Here are some examples:
1.
InputConnection ic = getCurrentInputConnection();
2.
long
eventTime = SystemClock.uptimeMillis();
3.
ic.sendKeyEvent(
new
KeyEvent(eventTime, eventTime,
4.
KeyEvent.ACTION_DOWN, keyEventCode,
0
,
0
,
0
,
0
,
5.
KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
6.
ic.sendKeyEvent(
new
KeyEvent(SystemClock.uptimeMillis(), eventTime,
7.
KeyEvent.ACTION_UP,
keyEventCode,
0
,
0
,
0
,
0
,
8.
KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
Or use the convenience method:
1.
InputMethodService.sendDownUpKeyEvents(keyEventCode);
Note : It is recommended to use the above method for certain fields such as phone number fields
because of filters that may be applied to the text after each key press. Return key and delete key should also be sent as raw key events for certain input types,as applications may be watching for specific key events in order to perform an action.
When editing text in a text field,some of the more useful methods on android.view.inputmethod.InputConnection are:
getTextBeforeCursor()
getTextAfterCursor()
deleteSurroundingText()
commitText()
For example,let's say the text "Fell" is to the left of the cursor. And you want to replace it with "Hello!":
1.
InputConnection ic = getCurrentInputConnection();
2.
ic.deleteSurroundingText(
4
,
0
);
3.
ic.commitText(
"Hello"
,
1
);
4.
ic.commitText(
"!"
,
1
);
Composing text before committing
If your input method does some kind of text prediction or requires multiple steps to compose a word or glyph,you can show the progress in the text field until the user commits the word and then you can replace the partial composition with the completed text.
The text that is being composed will be highlighted in the text field in some fashion,such as an underline.
1.
InputConnection ic = getCurrentInputConnection();
2.
ic.setComposingText(
"Composi"
,
1
);
3.
...
4.
ic.setComposingText(
"Composin"
,
1
);
5.
...
6.
ic.commitText(
"Composing "
,
1
);
![](http://news.wangmeng.cn/attachments/201008/1282200368522.png)
Intercepting hard key events
Even though the input method window doesn't have explicit focus,it receives hard key events first and can choose to consume them or forward them along to the application. For instance,you may want to consume the directional keys to navigate within your UI
for candidate selection during composition. Or you may want to trap the back key to dismiss any popups originating from the input method window. To intercept hard keys,override InputMethodService.onKeyDown() and InputMethodService.onKeyUp(). Remember to call
super.onKey * if you don't want to consume a certain key yourself.
Other considerations
Provide a way for the user to easily bring up any associated settings directly from the input method UI
Provide a way for the user to switch to a different input method (multiple input methods may be installed) directly from the input method UI.
Bring up the UI quickly - preload or lazy-load any large resources so that the user sees the input method quickly on tapping on a text field. And cache any resources and views for subsequent invocations of the input method.
On the flip side,any large memory allocations should be released soon after the input method window is hidden so that applications can have sufficient memory to run. Consider using a delayed message to release resources if the input method is in a hidden state
for a few seconds.
Make sure that most common characters can be entered using the input method,as users may use punctuation in passwords or user names and they shouldn't be stuck in a situation where they can't enter a certain character in order to gain access into a password-locked
device.
Samples
For a real world example,with support for multiple input types and text prediction,see LatinIME source code . The Android 1.5 SDK also includes a SoftKeyboard sample as well
相关文章推荐
- 【移动开发】 Android隐藏输入法软键盘的一些说明
- 【移动开发】 Android隐藏输入法软键盘的一些说明
- android开发,以记住用户登录密码为例说明SharedPreferences的简单应用
- android开发,以记住用户登录密码为例说明SharedPreferences的简单应用
- Android 开发实例教程 零基础学习 1小时入门 图解说明教程 化复杂为简单 配置好的android开发环境 带实例
- Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
- 安卓(android)开发框架的说明2:基于Android Studio的安卓工程开发目录介绍
- Android开发学习之路--UI之简单聊天界面
- java EE技术体系——CLF平台API开发注意事项(4)——API生命周期治理简单说明
- 值得推荐的android开发框架简单介绍
- 微信开发环境配置以及开发基础PHP简单说明
- Android开发学习之路--UI之简单聊天界面
- 深入浅出 - Android系统移植与平台开发(十) - led HAL简单设计案例分析
- 【开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位...
- Android开发的一些简单的总结
- 【Android游戏开发十七】让玩家自定义手势玩转Android游戏!—Android Gesture之【输入法手势技术】
- android开发 界面设计 图片资源设计 尺寸说明
- android 2D游戏开发,引擎设计(四) 造型类Model的设计。简单进度条
- 简单谈谈eclipse下搭建PhoneGap环境来开发Android程序
- Android开发:史上最简单方便的viewpager加indicator的方法