Android输入法开发
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输入法开发的更多相关文章
- 为 Android 平台开发一个输入法
学习目标: 实现新的输入法 学习目的: 掌握Android输入法框架 学习收获: Android 1.5 新特色之一就是输入法框架(Input Method Framework,IMF),正是它的出现 ...
- 【Android 应用开发】 Ubuntu 安装 Android Studio (旧版本|仅作参考)
. 果断换Ubuntu了, Ubuntu的截图效果不好, 不能设置阴影 ... 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article ...
- Android输入法架构学习总结
此文为本人学习输入法之后所做的一个总结报告.与大家分享. 安卓输入法框架(Input Method Framework)IMF 一.输入法框架简介 自Android平台1.5版本以后,Google开放 ...
- Android TV 开发 (1)
本文来自网易云社区 作者:孙有军 前言 这里主要记录几个TV问题的解决方案,如果对这个不感兴趣的其实就不用往下看了. 这几天有一个需求就是要求出一个TV版本的app,之前没有具体的了解Tv版的app有 ...
- Android 系统开发做什么?
题外话 18 年我从 Android 应用开发转 Framework 层开发了,从此开启了 996 幸福生活,博客技术文更新基本停滞了,被工作占据了过多的精力,实在没时间像以前一样拟稿.写作,实践.反 ...
- Android N开发 你需要知道的一切
title: Android N开发 你需要知道的一切 tags: Android N,Android7.0,Android --- 转载请注明出处:http://www.cnblogs.com/yi ...
- Android游戏开发实践(1)之NDK与JNI开发03
Android游戏开发实践(1)之NDK与JNI开发03 前面已经分享了两篇有关Android平台NDK与JNI开发相关的内容.以下列举前面两篇的链接地址,感兴趣的可以再回顾下.那么,这篇继续这个小专 ...
- Android游戏开发实践(1)之NDK与JNI开发01
Android游戏开发实践(1)之NDK与JNI开发01 NDK是Native Developement Kit的缩写,顾名思义,NDK是Google提供的一套原生Java代码与本地C/C++代码&q ...
- Android游戏开发实践(1)之NDK与JNI开发02
Android游戏开发实践(1)之NDK与JNI开发02 承接上篇Android游戏开发实践(1)之NDK与JNI开发01分享完JNI的基础和简要开发流程之后,再来分享下在Android环境下的JNI ...
随机推荐
- poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14106 Ac ...
- NewSQL——优化的SQL存储引擎(TokuDB, MemSQL)+?
NewSQL 是对各种新的可扩展/高性能数据库的简称,这类数据库不仅具有NoSQL对海量数据的存储管理能力,还保持了传统数据库支持ACID和SQL等特性. NewSQL 是指这样一类新式的关系型数据库 ...
- Qt 线程基础(Thread Basics的翻译,线程的五种使用情况)
Qt 线程基础(QThread.QtConcurrent等) 转载自:http://blog.csdn.net/dbzhang800/article/details/6554104 昨晚看Qt的Man ...
- UVa10886 Standard Deviation
留坑(p.345) 这是什么意思 暴力? 然而那些有两个人跑的那么快是为什么?(有个人竟然是陈锋...)
- 【Android - 框架】之MVP模式的使用
提起MVP架构模式,大家可能首先想到的是它的"前辈"MVC模式.MVC由Model.View.Controller组成,请求从Controller进入后进行业务判断,然后交给Mod ...
- DataTable无法使用AsEnumerable ()的解决办法
本人定义了DataSet后将表1赋给datatable,在写linq时调用datatable.asenumerable(),但报datatable不包含asenumerable的定义,求高手指点.Sy ...
- C# 自动登录网页,浏览页面【转载】
需求:客户的数据同时存在在另外一个不可控的系统中,需要和当前系统同步. 思路:自动登录另外一个系统,然后抓取数据,同步到本系统中. 技术点:模拟用户登录:保存登录状态:抓取数据 /// <sum ...
- 【LeetCode】Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [转][JAVA]定时任务之-Quartz使用篇
[BAT][JAVA]定时任务之-Quartz使用篇 定时任务之-Quartz使用篇 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与 ...
- qt 学习之路 :QML 语法
前面我们已经见识过 QML 文档.一个 QML 文档分为 import 和对象声明两部分.如果你要使用 Qt Quick,就需要 import QtQuick 2.QML 是一种声明语言,用于描述程序 ...