简介:最近因为项目需求,需要实现自定义输入法;本来想安卓技术发展这么多年了,已经到了迟暮的阶段;应该找点资料学习学习是比较简单的,但是找了一圈大失所望,不是千篇一律的“死文章”就是有头无尾,说的不明不白。于是自己只好慢慢摸索了大概一周左右,实现了输入法的雏形,此输入法的功能有:
1、字母符号键盘
2、数字键盘
3、符号键盘
4、键盘之间可切换
当然其中还有很多细节之处无法在此展开说,还是耗费了我一些时间的。
一、输入法实现步骤
项目的创建什么的,不在此说明
1.1、创建类,实现InputMethodService类,并实现其方法。
public class NumPadIME extends InputMethodService
implements KeyboardView.OnKeyboardActionListener {
}
1.2、创建并实现输入法界面
@Override
public View onCreateInputView() {
keyboardView = (KeyboardView) getLayoutInflater()
.inflate(R.layout.input, null);
keyboardView.setKeyboard(numpadKeyboard);
keyboardView.setOnKeyboardActionListener(this);
return keyboardView;
}
在onCreateInputView方法中,实例化keyboardView对象,并创建其界面。
<?xml version="1.0" encoding="utf-8"?>
<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:keyBackground="@drawable/key_bg"
android:keyTextColor="@color/black"
android:labelTextSize="12sp"
android:keyTextSize="16sp"
style="@style/DynamicKeyStyle"
/>
在此xml中你就可以定义你的输入法界面样式风格。
1.3 输入法界面设计及布局
在Android平台上,界面布局通常使用XML来定义,输入法界面也不例外。Android为开发者提供了丰富的布局组件和控件。最基本的控件包括TextView用于显示文本,EditText用于输入文本,以及Button用于触发事件。对于输入法来说, KeyboardView 是一个专用的控件,用于绘制和管理虚拟键盘的视图。它允许开发者通过自定义XML布局来设计键盘。
以下是一个数字键盘的xml布局,提供参考
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="20%p"
android:keyHeight="50dp"
android:horizontalGap="2dp"
android:verticalGap="2dp">
<!-- 第0行 -->
<Row>
<Key android:codes="46" android:keyLabel="."/>
<Key android:codes="44" android:keyLabel=","/>
<Key android:codes="45" android:keyLabel="-"/>
<Key android:codes="43" android:keyLabel="+"/>
<Key android:codes="-5" android:keyIcon="@drawable/ic_keyboard_confrim" />
</Row>
<!-- 第一行 -->
<Row>
<Key android:codes="49" android:keyLabel="1"/>
<Key android:codes="50" android:keyLabel="2"/>
<Key android:codes="51" android:keyLabel="3" />
<Key android:codes="9001" android:keyLabel="中"/>
<Key android:codes="9005" android:keyLabel="abc"/> />
</Row>
<!-- 第二行 -->
<Row>
<Key android:codes="52" android:keyLabel="4" />
<Key android:codes="53" android:keyLabel="5"/>
<Key android:codes="54" android:keyLabel="6"/>
<Key android:codes="48" android:keyLabel="0"/>
<Key android:codes="9000" android:keyLabel="!?#"/>
</Row>
<!-- 第三行 -->
<Row>
<Key android:codes="55" android:keyLabel="7"/>
<Key android:codes="56" android:keyLabel="8"/>
<Key android:codes="57" android:keyLabel="9"/>
<Key android:codes="32" android:keyLabel="空格"/>
<Key android:codes="-3" android:keyIcon="@drawable/ic_keyboard_del"/>
</Row>
</Keyboard>
以上xml中将“确认”和“删除”按钮采用图标形式展示,开始时我使用android:keyIcon设置图标一直没有生效,其中都有很多细节。
1.4 实例化各键盘对象
前面介绍过我们的输入法包括了数字键盘、符号键盘、字母键盘。所以我们在此需要分别对他们进行初始化。
private void initKeyboards() {
numpadKeyboard = new Keyboard(this, R.xml.enpad);
symbolKeyboard = new Keyboard(this, R.xml.keyboard_page_1);
numberKeyboard = new Keyboard(this, R.xml.number);
// 加载三个页面
pages[0] = new Keyboard(this, R.xml.keyboard_page_1);
pages[1] = new Keyboard(this, R.xml.keyboard_page_2);
pages[2] = new Keyboard(this, R.xml.keyboard_page_3);
}
以上就是创建输入法对象的方法,我的符号键盘是通过翻页实现,总共有3页。
1.5 处理key事件
当我们点击我们自定义的键盘之后,需要额外的处理一些自己的逻辑;比如:删除、确认、切换键盘、大小写等。这个时候我们就需要对这些按钮进行业务逻辑处理。
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection ic = getCurrentInputConnection();
if (ic == null) return;
if(currentMode == InputMode.EN) {
if (handleDynamicKeys(primaryCode)) {
return;
}
}
switch(primaryCode) {
case Keyboard.KEYCODE_CANCEL:
//删除
handleDelete(ic);
break;
case Keyboard.KEYCODE_MODE_CHANGE:
//中英文切换
currentMode = InputMode.EN;
toggleKeyboard();
break;
case 9000:
// 符号键盘
currentMode = InputMode.SYM;
toggleKeyboard();
break;
case 9005:
// 字母键盘
currentMode = InputMode.EN;
toggleKeyboard();
break;
case 9001:
// 中英切换
toggleLanguage();
break;
case 9002:
// 大小写切换
break;
case 9003:
// 数字键盘
currentMode = InputMode.NUM;
toggleKeyboard();
break;
case 1000:
//符号翻页
currentPage = (currentPage + 1) % PAGE_COUNT;
updateKeyboard();
break;
case -5:
// 确定
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
break;
default:
handleCharacter(primaryCode, ic);
}
}
以上就是我的特殊按钮处理事件。
1.6 中文拼音
中文拼音输入法,暂时还没有完全实现,但是思路已经无疑;首先我们要知道汉字与字母之间的桥梁就是拼音;而拼音就是字母,所以我们只要将字母组成拼音,然后将拼音转化为中文即可;
大概思路
1、将拼音的数据库导入项目中
2、将输入的字母以拼音的形式在数据库中查询中文
当然此处是最关键的一步,因为汉字非常多,通过拼音进行模糊查询的时候会涉及到性能问题。
3、将查询出来的文字显示在键盘之上
4、获取所选择的汉字。
以上是中文的拼音输入思路,后续我会继续更新实现。
二、输入法效果
2.1、数字键盘
数字键盘实现起来比较简单。无非就是进行界面布局,通过key事件获取点击的数字。
数字键盘第一行,带有常用的符号,同时确认和删除采用图标实现。
2.2、字母键盘
我这里采用的是九宫格字母键盘,如果大家使用26宫格字母键盘,其实更简单。原因就在于,九宫格字母键盘在点击之后还要区分点击的是哪个字母,而26宫格字母键盘,不需要区分;比如点击“abc”之后,还需要在abc中选择其中一个进行确认。
图2就是点击其中一个字母之后另取一行,显示其所选择的3个或4个字母。这种场景非常适合在小屏幕设备上使用。个人觉得这种方式非常实用。
2.3、符号键盘
我们知道,键盘中符号非常多,一页是放不下的,一般是使用滑动显示功能符号,但我在此是使用“翻页”按钮进行翻页实现,符号共三页,通过翻页按钮实现其功能。
总结
以上就是我实现输入法的大概思路以及效果。如需获取源代码请联系我vx;提前声明此源码需要为知识付费一包yan钱就能为你提供完整的思路,节省你的时间,提升你的效率;时间和效率最宝贵啊,探索不易啊,介意着勿扰。
[添加时请备注"CSDN读者",可快速通过审核」