1. 概念

* IMF: 输入法框架(Input Method Framework)

* IM: 输入法(Input Method)

* IMS: 输入法服务(Input Method Service)

* IMMS: 输入法服务管理器(Input Method Manager Service), system process的一部分,系统中只有一个该服务的实例

* IMM: 输入法管理器(Input Method Manager), 每个客户端进程包含一个该实例

* IME: 泛指一个具体的输入法APK,(Input Method Engine),包括其内部的IMS和各种Binder

2. 核心思想

* 以Service方式运行具体的输入法

* 在Service中创建输入法窗口,并把输入内容传递到EditText框中

3. IME两个Binder

* IMS对应的Binder, IMMS通过该Binder去控制输入法,如: 显示、隐藏等

* 专门供客户端调用的Binder, 该对象用于 在输入的过程中,客户端 将事件 传给输入法

4. IME几个要素

4.1 IME 与 EditText框 交互 —— InputConnection

4.2 IME 与 用户 交互

* InputView: 获取 用户输入

* Candidate View:根据用户输入形成候选词栏 给用户反馈

5. 动手写一个IME

* 新建一个工程CarloZ IME, 创建一个Java类 CarloZIME.java, 让它继承 InputMethodService;

* 新建一个SettingActivity,作为IME的设定

* 在res/xml 下新建 method.xml, 将下面的代码 修改以后拷贝进去(im_is_default 默认是true)

<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The attributes in this XML file provide configuration information -->
<!-- for the Search Manager. -->
<input-method xmlns:android="http://schemas.android.com/apk/res/android"
android:isDefault="@bool/im_is_default"
android:settingsActivity="com.carloz.inputmethod.setting.CarloZIMESettingActivity" > </input-method>

* 在AndroidManifest.xml中配置如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.carloz.inputmethod"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<service
android:name="com.carloz.inputmethod.CarloZIME"
android:label="@string/app_name"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter android:priority="1">
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data
android:name="android.view.im"
android:resource="@xml/method" />
</service> <activity
android:name=".setting.CarloZIMESettingActivity"
android:label="@string/title_activity_carlo_zimesetting">
</activity> </application> </manifest>

此时一个最简单的输入法就配置完了

问题 1: 如何与EditText框 交互

问题2: 如何获取用户输入

接下来的的我们来解决这俩问题

6. IME 与 EditText交互 —— InputConnection

因为CarloZIME.java继承了 InputMethodService, 所以它拥有一个方法 getCurrentInputConnection()

前面我们说过,InputConnection 是用来跟 EditText框交互的,说到这里你肯定已经懂了,没错,就是这样

InputConnection ic = getCurrentInputConnection();
ic.commitText("Hello CarloZ", 0);

在合适的地方调用上述代码,就会向EditText框中 提交 Hello CarloZ 这个字符串。

InputConnection 是一个接口,它与EditText交互的方式可以分为3种: 读取,插入,替换

(1) 获取 EditText 框中已有的字符, 方式分为 以相对光标的位置获取 和 使用绝对位置获取

* getTextBeforeCursor(n, flag); 获取光标前面的n个字符

* getTextAfterCursor(n, flag); 获取光标后面的n个字符

* getExtractedText(request, flags); 获取当前Edit框所有文字

* setSelection(start, end); 设置绝对位置,从start到end处

* getSelectedText(flags); 获取绝对区域的文字,该区域由setSelection决定

(2)向EditText框中 添加删除 字符

* deleteSurroudingText(left, right): 删除当前光标处 前面left个字符 和 后面 right个字符

* commitText(text, pos); 传递text给 EditText, 并且移动 光标到pos处

* commitCompletion(CompletionInfo text); 完成提交,一般是切换到另一个EditText框之前调用,从而使得客户端在此做点什么

(3)替换 EditText中的字符

* setComposingRegion(start, end); 设置替换区域为 从 start到end处

* setComposingText(text, pos); 把替换区域的文字替换为 text,并把光标移动到pos 处

* finishComposingText(); 可以重复设置替换区域并进行替换,完成后,使用该API 通知客户端

(4)特殊功能 getCursorCapsMode(reqModes), 用于获取 当前 光标处的大写模式

7. 与 用户交互

现在我们的IME还没有View, 所以 虽然输入法在运行,但是你什么都看不到,用户自然也就无法 输入任何东西

Android输入法开发的更多相关文章

  1. 为 Android 平台开发一个输入法

    学习目标: 实现新的输入法 学习目的: 掌握Android输入法框架 学习收获: Android 1.5 新特色之一就是输入法框架(Input Method Framework,IMF),正是它的出现 ...

  2. 【Android 应用开发】 Ubuntu 安装 Android Studio (旧版本|仅作参考)

    . 果断换Ubuntu了, Ubuntu的截图效果不好, 不能设置阴影 ... 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article ...

  3. Android输入法架构学习总结

    此文为本人学习输入法之后所做的一个总结报告.与大家分享. 安卓输入法框架(Input Method Framework)IMF 一.输入法框架简介 自Android平台1.5版本以后,Google开放 ...

  4. Android TV 开发 (1)

    本文来自网易云社区 作者:孙有军 前言 这里主要记录几个TV问题的解决方案,如果对这个不感兴趣的其实就不用往下看了. 这几天有一个需求就是要求出一个TV版本的app,之前没有具体的了解Tv版的app有 ...

  5. Android 系统开发做什么?

    题外话 18 年我从 Android 应用开发转 Framework 层开发了,从此开启了 996 幸福生活,博客技术文更新基本停滞了,被工作占据了过多的精力,实在没时间像以前一样拟稿.写作,实践.反 ...

  6. Android N开发 你需要知道的一切

    title: Android N开发 你需要知道的一切 tags: Android N,Android7.0,Android --- 转载请注明出处:http://www.cnblogs.com/yi ...

  7. Android游戏开发实践(1)之NDK与JNI开发03

    Android游戏开发实践(1)之NDK与JNI开发03 前面已经分享了两篇有关Android平台NDK与JNI开发相关的内容.以下列举前面两篇的链接地址,感兴趣的可以再回顾下.那么,这篇继续这个小专 ...

  8. Android游戏开发实践(1)之NDK与JNI开发01

    Android游戏开发实践(1)之NDK与JNI开发01 NDK是Native Developement Kit的缩写,顾名思义,NDK是Google提供的一套原生Java代码与本地C/C++代码&q ...

  9. Android游戏开发实践(1)之NDK与JNI开发02

    Android游戏开发实践(1)之NDK与JNI开发02 承接上篇Android游戏开发实践(1)之NDK与JNI开发01分享完JNI的基础和简要开发流程之后,再来分享下在Android环境下的JNI ...

随机推荐

  1. hdu 4762 Cut the Cake概率公式

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4762 题目大意:一个圆形蛋糕,现在要分成M个相同的扇形,有n个草莓,求n个草莓都在同一个扇形上的概率. ...

  2. lightoj 1027 简单概率dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 #include<cstdio> #include<cstri ...

  3. django运行django-admin.py无法创建网站

    安装django的步骤: 1.安装python,选择默认安装在c盘即可.设置环境变量path,值添加python的安装路径. 2.下载ez_setup.py,下载地址:http://peak.tele ...

  4. Nearly prime numbers - SGU 113(素数)

    题目大意:判断一个数是否是两个素数的乘积,如果是,输出Yes,否则No. 分析:先打表求出来一部分素因子,用素数对素数判定还是比较快的. 代码如下: ========================= ...

  5. 常用shell变量$#,$@,$0,$1,$2等

    常用shell变量$#,$@,$0,$1,$2的含义解释: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代码(返 ...

  6. 使用Calendar获取近三年的财务信息

    1.

  7. linux —— shell 编程(编程语法)

    导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 再识变量 函数 条件语句 循环语句 ...

  8. asp.net 动态添加多附件上传.

    最近有人问起动态多文件上传,想要做到类似于邮箱添加附件的效果,这个功能其实比较简单,就是往form中添加file元素.在用户选择完文件后,再添加一个file控件,由于file控件过多,视觉上不好看,所 ...

  9. TREEVIEW节点拖拽

    http://files.cnblogs.com/xe2011/TreeView_Drag_and_Drop.rar       假设把A节点往B节点上拖拽 那么  A 为Node1,B为Node2 ...

  10. wdlinux mysql innodb的安装

    mysql innodb的安装 wget -c http://down.wdlinux.cn/in/mysql_innodb_ins.sh chmod 755 mysql_innodb_ins.sh ...