admin管理员组

文章数量:1564206

如何自定义 安卓输入法 和 键盘

1.首先有几个关键类

1.InputMethodService 2.Keyboard 3.KeyboardView

1.1 InputMethodService

看下这个类的介绍

图片.png

InputMethodService provides a standard implementation of an InputMethod

作用 提供一个标准键盘实现 balabala.....

输入法生命周期.png

请参考 http://blog.csdn/weijinqian0/article/details/76906317

1.2.Keyboard 源码分析

图片.png

xmL 定义键盘的属性 : 键位宽/高/水平间距/垂直间距/按键文字图标/键值.....

键盘的UI样式就在这个类里面定义,准确的说 是在keyboard加载的xml文件中定义

构造方法

传入键盘布局文件id

/**

* Creates a keyboard from the given xml key layout file.

* @param context the application or service context

* @param xmlLayoutResId the resource file that contains the keyboard layout and keys.

*/

public Keyboard(Context context, int xmlLayoutResId) {

this(context, xmlLayoutResId, 0);

}

/**

* Creates a keyboard from the given xml key layout file. Weeds out rows

* that have a keyboard mode defined but don't match the specified mode.

* @param context the application or service context

* @param xmlLayoutResId the resource file that contains the keyboard layout and keys.

* @param modeId keyboard mode identifier

* @param width sets width of keyboard

* @param height sets height of keyboard

*/

public Keyboard(Context context, @XmlRes int xmlLayoutResId, int modeId, int width,

int height) {

mDisplayWidth = width;

mDisplayHeight = height;

mDefaultHorizontalGap = 0;

mDefaultWidth = mDisplayWidth / 10;

mDefaultVerticalGap = 0;

mDefaultHeight = mDefaultWidth;

mKeys = new ArrayList();

mModifierKeys = new ArrayList();

mKeyboardMode = modeId;

//加载键盘

loadKeyboard(context, context.getResources().getXml(xmlLayoutResId));

}

xml 解析键盘布局

private void loadKeyboard(Context context, XmlResourceParser parser) {

boolean inKey = false;

boolean inRow = false;

boolean leftMostKey = false;

int row = 0;

int x = 0;

int y = 0;

Key key = null;

Row currentRow = null;

Resources res = context.getResources();

boolean skipRow = false;

try {

int event;

while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {

if (event == XmlResourceParser.START_TAG) {

String tag = parser.getName();

if (TAG_ROW.equals(tag)) {

inRow = true;

x = 0;

currentRow = createRowFromXml(res, parser);

rows.add(currentRow);

skipRow = currentRow.mode != 0 && currentRow.mode != mKeyboardMode;

if (skipRow) {

skipToEndOfRow(parser);

inRow = false;

}

} else if (TAG_KEY.equals(tag)) {

inKey = true;

key = createKeyFromXml(res,

本文标签: 自定义输入法布局键盘android