输入法的例子和源代码看了不少时间了,看得头很晕,很郁闷。静下心来把整个代码想了一遍,其实大部分代码都在处理绘制界面,以及事件的处理,核心代码很少,都被底层封装得很完美了。
先看看一般输入法的界面:

分为两个部分,上部分是候选窗口(CandidateView),用来显示候选词,现在常用的输入法都有这个功能,如在搜狗、google输入法中输入abc,输入框中就会显示很多相关联的中文词。下部分就是软键盘了,这个没什么好说的。

输入法中核心类是InputMethodService
其次就是:CandidateViewKeyboardView

CandidateView为候选窗口,InputMethodService在启动过程中会调用接口public View onCreateCandidatesView() ,在这个方法

中把CandidateView对象返回后,InputMethodService内部会将其布局到相应的位置。

在android中没有CandidateView父类,得自己从头写,一般的做法是:

通过方法public void setService(InputMethodService listener) 将Service类传进来,然后再通过public void setSuggestions(List<String> suggestions…) 方法将候选词列表传递过来,CandidateView将其显示到界面上,用户选择结束后,再通过service的方法pickSuggestionManually(mSelectedIndex) ,将选择的候选词在列表中的序号传递回去。至此, CandidateView 就完成了它神圣的使命。

android中KeyboardView有一个通用类,继承它可以通过简单的配置文件就显示出很专业软键盘。在源代码中,它绝大部分代码都在做绘制工作和事件处理,不过就其本质功能来说是相当地简单,用户摁下软键盘上的某个键后,它把这个键所代表的值传递给InputMethodService类也完成了它的使命。InputMethodService在public View onCreateInputView() 方法中获得该View。

InputMethodService就是输入法的核心了,该类是一个Service,跟其它默默无闻的Service不同的是,它是一个带有View的Service。其内部有几个个重要的接口:

InputMethodImpl

InputMethodSessionImpl

InputConnection

InputMethodService通过这几个个类跟系统和输入框进行交互的。

输入框从InputMethodService获取信息是通过InputConnection来实现的, 在启动输入法时,InputConnection由客户端控件创建,并传递给输入法应用,由输入法应用调用,进行信息反馈

InputMethod接口定义了一套操纵输入法应用的方法。如,bindInput, hideInput, startInput等。为了系统安全,这类接口只有系统可以访问,客户端控件无法直接调用这个接口。所有的输入法应用都需要客户端控件具有 BIND_INPUT_METHOD权限,作为系统的安全机制,否则将无法与输入法服务交互。

InputMethodSession作为InputMethod的辅助接口类,为客户端控件开放了可直接调用的函数接口。包括向输入法应用分发键盘事件,更新光标位置,更新编辑区域内选择的问题信息等。客户端控件通过IIputMethodSession对于输入法应用的交互是单向的,即只能向输入法应用传递信息,无法获取信息

以上几个点是从网上copy过来的,感觉这几点对于理解InputMethodService特别有用。

代码看得太多反而看不清本质,这几个类中最实用的是InputConnection的

public boolean commitText(CharSequence text, int newCursorPosition) 。

通过KeyboardView和CandidateView, InputMethodService类已经获得了想要的内容,然后通过这个方法把值传递给输入框。

按奈不住心中读源代码的郁闷,先来写一个输入法发泄一下:

先来一个CandidateView,设想的布局如下:

这个View中不进行任何自绘制,用android现有的View,两边各一个按钮(Button),用来滚动多个候选词,中间显示候选词(TextView),为了方便CandidateView继承RelativeLayout的内部类,便于加入子控件和控制,setService和 setSuggestions两个方法可以不用,反正是内部类,不过为了配合上面的说明,思量再三还是加上了:

public class helloIme extends InputMethodService {

class CandidateView extends RelativeLayout{
    TextView tv;                 // 中间显示候选词
    Button btLeft, btRight; // 左右按钮
    helloIme listener;         // helloIme 用于返回选中的 候选词下标
    List<String> suggestions; // 候选词列表, KeyboardView 不同的键按下后会设置相关的列表
    int mSelectedIndex = -1;  // 当前 候选词下标
   
public CandidateView(Context context) {
    super(context);
   
    tv = new TextView(context);
    tv.setId(1);
    RelativeLayout.LayoutParams lpCenter = new RelativeLayout.LayoutParams(200, ViewGroup.LayoutParams.WRAP_CONTENT);
    lpCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
    addView(tv, lpCenter);
    tv.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            listener.pickSuggestionManually(mSelectedIndex);
        }
    });
   
    btLeft = new Button(context);
    btLeft.setText("<");
    btLeft.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0) {
            mSelectedIndex = mSelectedIndex > 0 ? (mSelectedIndex - 1) : 0;
            tv.setText(suggestions.get(mSelectedIndex));
        }
    });
   
    RelativeLayout.LayoutParams lpLeft = new RelativeLayout.LayoutParams(60, ViewGroup.LayoutParams.WRAP_CONTENT);
    lpLeft.addRule(RelativeLayout.LEFT_OF, 1);
    addView(btLeft, lpLeft);
   
    btRight = new Button(context);
    btRight.setText(">");
    btRight.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            mSelectedIndex = mSelectedIndex >= suggestions.size() - 1 ? suggestions.size() - 1 : mSelectedIndex + 1;
            tv.setText(suggestions.get(mSelectedIndex));
        }
    });
   
    RelativeLayout.LayoutParams lpRight = new RelativeLayout.LayoutParams(60, ViewGroup.LayoutParams.WRAP_CONTENT);
    lpRight.addRule(RelativeLayout.RIGHT_OF, 1);
    addView(btRight, lpRight);
}

public void setService(helloIme listener){
    this.listener = listener;
}

public void setSuggestions(List<String> suggestions) {
    mSelectedIndex = 0;
    tv.setText(suggestions.get(mSelectedIndex));
    this.suggestions = suggestions;
}
}

上面最重要的是粗体的那两行,View的布局还是花费了很多代码:(

KeyboardView的布局预想如下:

就两个按钮,点if时往输入框中输出if(){}, if(){}else if(){}...,whie时往输入框中输出whie(){},这个类同样是继承于RelativeLayout的内部类:

class KeyboardView extends RelativeLayout{
  public KeyboardView(Context context) {
        super(context);
       
        Button btIf = new Button(context);
        btIf.setText("if");
        btIf.setId(1);
        RelativeLayout.LayoutParams lpIf = new RelativeLayout.LayoutParams(100, 50);
        lpIf.addRule(RelativeLayout.CENTER_HORIZONTAL);
       
        btIf.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                setCandidatesViewShown(true); // 显示 CandidateView
                helloIme.this.onKey("if"); // 将点击按钮的值传回给 InputMethodService
            }
        });
        addView(btIf, lpIf);
       
        Button btWhile = new Button(context);
        btWhile.setText("while");
        RelativeLayout.LayoutParams lpWhile = new RelativeLayout.LayoutParams(100, 50);
        lpWhile.addRule(RelativeLayout.BELOW , 1);
        lpWhile.addRule(RelativeLayout.ALIGN_LEFT, 1);
       
        btWhile.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                setCandidatesViewShown(true);
                helloIme.this.onKey("while");

            }
        });
        addView(btWhile, lpWhile);
    }
}

CandidateView默认是不显示的,所以需要调用InputMethodService 的setCandidatesViewShown()方法。

接下来把helloIme的代码贴出来:

public class helloIme extends InputMethodService {
    private List<String>suggestionlist; // 当前候选词表
    private Hashtable<String, List<String>> data; // 词典数据
    private KeyboardView mkeyView;
    private CandidateView mCandView;
   
    public void onInitializeInterface() { //InputMethodService在启动时,系统会调用该方法,具体内容下回再表
        // 初始化 词典数据

data = new Hashtable<String, List<String>>();
        List<String> list = new ArrayList<String>();
        list.add("if(){}");
        list.add("if(){}else if(){}");
        list.add("if(){}else{}");
        data.put("if", list);
       
        list = new ArrayList<String>();
        list.add("while(){}");
        data.put("while", list);
    }
   
    public View onCreateInputView() {
        mkeyView = new KeyboardView(this);
        return mkeyView;
    }

public View onCreateCandidatesView() {
        mCandView = new CandidateView(this);
        mCandView.setService(this);
        return mCandView;
    }
   
    public void pickSuggestionManually(int mSelectedIndex){
        getCurrentInputConnection().commitText(suggestionlist.get(mSelectedIndex), 0); // 往输入框输出内容
        setCandidatesViewShown(false); // 隐藏 CandidatesView
    }
   
    public void onKey(CharSequence text){
        // 根据按下的按钮设置候选词列表

suggestionlist = data.get(text);
        mCandView.setSuggestions(suggestionlist);
    }

class KeyboardView extends RelativeLayout{

//......

}

class CandidateView extends RelativeLayout{

//......

}

}

代码写完,再来写配置文件,

在res目录下面建立一个新目录xml,然后创建一个method.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- The attributes in this XML file provide configuration information -->
<!-- for the Search Manager. -->

<input-method
    xmlns:android="http://schemas.android.com/apk/res/android" />

设置Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="test.helloIme"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <service android:name="helloIme"
        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>

    </application>
    <uses-sdk android:minSdkVersion="5" />

</manifest>

直接运行程序,eclipse输出如下Log:

[2010-08-25 17:16:48 - helloIme]Installing helloIme.apk...
[2010-08-25 17:16:50 - helloIme]Success!
[2010-08-25 17:16:50 - helloIme]/helloIme/bin/helloIme.apk installed on device
[2010-08-25 17:16:50 - helloIme]Done!


嗯,安装成功了!呵呵,革命尚未成功,还需在模拟器上进行设置:

点击settings->Language & keyboard,在下部出现了一个test,右边有个checkbox,选上它。

找一个有输入框的应用,最简单到写短消息的画面,左键长按输入框,会弹出一个输入法选择提示框,点进去就会看到刚才创建的输入法了,点击右边的单选框,oh,my ime,漂亮的hello输入法就展现在面前了:

android果然很强大啊。

android 之输入法相关推荐

  1. android系统动态切换输入法,一种动态切换Android系统输入法的弹出模式的方法与流程...

    技术特征: 1.一种动态切换Android系统输入法的弹出模式的方法,其特征在于,包括步骤如下: (1)初始状态下,即软键盘和表情面板都未展开时,为表情面板设置一个默认高度,默认高度的取值范围是230 ...

  2. android输入法源码分析,基于Android的输入法的设计与实现.doc

    本 科 毕 业 论 文 基于Android的输入法的设计与实现 The Design and Realization of the Input Method Based on Android 学 院 ...

  3. android自动切换输入法,一种动态切换Android系统输入法的弹出模式的方法

    主权项: 1.一种动态切换Android系统输入法的弹出模式的方法,其特征在于,包括步骤如下:(1)初始状态下,即软键盘和表情面板都未展开时,为表情面板设置一个默认高度,默认高度的取值范围是230dp ...

  4. android 自定义输入法布局,Android自定义输入法使用方法

    Android自定义输入法使用方法 时间:2017-04-21     来源:Android开发学习网 对于Android用户而言,一般都会使用第三方的输入法.可是在输入密码时(尤其是支付相关的密码) ...

  5. android+平板+输入法,搜狗推首款Android3.0平板电脑输入法

    搜狗推首款Android3.0平板电脑输入法 2011年04月12日 12:19作者:厂商投稿编辑:孙莹莹文章出处:泡泡网原创 分享 搜狗率先推出首款完美支持Android 3.0平板专用版中文输入法 ...

  6. android 输入法更换_一种动态切换Android系统输入法的弹出模式的方法与流程

    本发明涉及一种Android系统利用动态切换输入法的弹出模式解决输入法跳闪抖动问题的方法,属于安卓系统技术领域. 背景技术: 随着Android系统的快速发展以及安卓手机的不断普及,基于Android ...

  7. android代码打开语言和输入法设置,Android默认输入法勾选多国语言,并默许其中一种语言...

    Android默认输入法勾选多国语言,并默认其中一种语言 1.首先在设备上调整输入法 设置>语言输入法>Android键盘(AOSP),在输入语言里勾选要选择的语言,比如选"英语 ...

  8. Android 默认输入法配置问题

    Android默认输入法的配置和两个关键词相关,都保存在SettingsProvider或者settings_secure.xml里面.一个是enabled_input_methods,表示配置的系统 ...

  9. android wear输入法,Moto360安装Analog Keyboard输入法教程详解

    Moto360智能手表是可以安装输入法的,小编比较推荐的一款输入法就是Analog Keyboard输入法.想在Moto360智能手表上安装输入法的朋友们来看看我机网小编带来的Moto360安装输入法 ...

  10. Android 显示输入法中的emoji表情以及String字符串转码

    Android 显示输入法中的emoji表情以及String字符串转码 实现用原生键盘输入表情并显示,有两种办法: 发送时将String字符串转换为Unicode编码字符串,显示接收的列表时用Unic ...

最新文章

  1. cmd指令大全指令_Linux 超全实用指令大全 | CSDN 博文精选
  2. solr配置-Schema.xml
  3. go语言生成自定义uuid_竟然不用写代码!一款可视化 Web 管理后台生成工具
  4. Navicat数据库备份还原
  5. MYsql可以做独立数据库_mysql-用于独立应用程序Winforms的数据库
  6. WPF中的触发器(Trigger)
  7. Autodesk云计算系列视频 --- 云计算与Civil 3D
  8. html5 coverflow,使用FancyCoverFlow实现3D无限循环切换视图
  9. uvc能支持多大分辨率_华为手环B5测评:该有的功能一个不缺,甚至还能变身蓝牙耳机...
  10. How to Fix Git Push Time Consuming Issue in a Big Team?
  11. php 5.2 thinkphp 3.2.2 cache,查询缓存-ThinkPHP3.2.3完全开发手册
  12. bzoj 1006: [HNOI2008]神奇的国度
  13. CSS 如何让li横向居中显示
  14. 数据说话!UCloud「硬刚」腾讯云,高性能 AMD 云主机哪家强?
  15. IBM ThinkPad错误代码列表
  16. java哈希表实现_Java实现简单哈希表详细代码
  17. Android开源代码解读のOnScrollListener实现ListView滚屏时不加载数据
  18. 内存图片IOS app启动动画的实现
  19. CCF推荐国际学术会议与学术期刊
  20. 杨校老师课堂之Hadoop环境搭建(一)

热门文章

  1. 吉他大横按的学习技巧总结
  2. 在线TSV转CSV工具
  3. 如何理解线性赋范空间、希尔伯特空间, 巴拿赫空间,拓扑空间
  4. 美团技术团队当家运营:美美正式出道啦(含福利)
  5. 【C/C++学院】0901-设计模式的汇总演练
  6. 【华人学者风采】李海洲 新加坡国立大学
  7. 学计算机小升初,小升初微机派位什么意思
  8. 吴恩达-机器学习-多元线性回归模型代码
  9. 基于Python实现的作家风格识别
  10. 直播预告|无监督学习的公平性研究——美国布兰迪斯大学助理教授刘洪甫