appium android数字字母切换键,Android自定义键盘:数字键盘和字母键盘

在项目中,产品对于输入方式会有特殊的要求,需要对输入方式增加特定的限制,这就需要采用自定义键盘。本文主要讲述数字键盘和字母键盘的自定义实现。

e90e7dfda122?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io

自定义键盘的实现步骤如下:

自定义CustomKeyboard, 继承自系统Keyboard,实现KeyboardView.OnKeyboardActionListener相关接口,以处理用户的点击回调;

自定义CustomKeyboardView, 继承自KeyboardView,实现自定义键盘绘制;

创建KeyboardManager, 用于处理自定义键盘的显示以及和输入UI的交互

自定义CustomKeyboard

Android系统Keyboard的构造方法如下:

/**

* 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) {

...

}

等等

其中,参数xmlLayoutResId是必须的,另外还可以通过计算系统键盘的高度来设定自定义键盘的高度。

xmlLayoutRes的格式如下:

android:keyHeight="49dp"

android:horizontalGap="0.1333%p"

android:verticalGap="1px"

xmlns:android="http://schemas.android.com/apk/res/android">

android:keyLabel="1" />

android:isRepeatable="true"/>

...

详细的数字键盘和字母键盘xmlLayoutRes资源文件可以从以下链接获取:

数字键盘xmlLayoutRes

字母键盘xmlLayoutRes

CustomKeyboard主要目的就是赋予xmlLayoutRes并实现特定按键的点击处理,其主要重载的方法是onKey(int primaryCode, int[] keyCodes)。详细代码如下:

public abstract class BaseKeyboard extends Keyboard implements KeyboardView.OnKeyboardActionListener{

@Override

public void onKey(int primaryCode, int[] keyCodes) {

if(null != mEditText && mEditText.hasFocus() && !handleSpecialKey(primaryCode)) {

Editable editable = mEditText.getText();

int start = mEditText.getSelectionStart();

int end = mEditText.getSelectionEnd();

if (end > start){

editable.delete(start,end);

}

if(primaryCode == KEYCODE_DELETE) {

if(!TextUtils.isEmpty(editable)) {

if(start > 0) {

editable.delete(start-1,start);

}

}

}else if(primaryCode == getKeyCode(R.integer.hide_keyboard)){

hideKeyboard();

}else {

editable.insert(start,Character.toString((char) primaryCode));

}

}

}

public abstract boolean handleSpecialKey(int primaryCode);

}

如上所示是BaseKeyboard,数字键盘和字母键盘需要继承它,并实现public abstract boolean handleSpecialKey(int primaryCode)方法。

自定义CustomKeyboardView

KeyboardView 是承载不同的keyboard并绘制keyboard, 是键盘布局的绘制板, 并与系统交互。通过继承KeyboardView自定义CustomKeyboardView,可以对按键样式实现自定义。考察KeyboardView的源码,发现其UI样式都是private类型,这就需要通过反射的方式获取特定的UI属性,并重新进行赋值,同时重载onDraw()方法,在onDraw()中重新绘制。

自定义键盘的UI效果如下:

e90e7dfda122?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io

数字键盘

e90e7dfda122?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io

字母键盘

创建KeyboardManager

主要处理以下功能逻辑:

绑定EditText和Keyboard,监听EditText的OnFocusChangeListener,处理键盘弹出和键盘掩藏;

处理系统键盘和自定义键盘之间的切换关系;

处理键盘区域其他自定义view的显示,比如需要让键盘自动搜索功能时,可在manager中进行相关处理

以绑定EditText为例:

public void bindToEditor(EditText editText, BaseKeyboard keyboard) {

hideSystemSoftKeyboard(editText);

editText.setTag(R.id.bind_keyboard_2_editor, keyboard);

if (keyboard.getKeyStyle() == null) {

keyboard.setKeyStyle(mDefaultKeyStyle);

}

editText.setOnFocusChangeListener(editorFocusChangeListener);

}

private final View.OnFocusChangeListener editorFocusChangeListener = new View.OnFocusChangeListener() {

@Override

public void onFocusChange(final View v, boolean hasFocus) {

if (v instanceof EditText) {

if (hasFocus) {

v.postDelayed(new Runnable() {

@Override

public void run() {

showSoftKeyboard((EditText) v);

}

},300);

} else {

hideSoftKeyboard();

}

}

}

};

public void showSoftKeyboard(EditText editText) {

mRootView.addOnLayoutChangeListener(mOnLayoutChangeListener);

BaseKeyboard keyboard = getBindKeyboard(editText);

if (keyboard == null) {

Log.e(TAG, "edit text not bind to keyboard");

return;

}

keyboard.setEditText(editText);

keyboard.setNextFocusView(mKeyboardWithSearchView.getEditText());

initKeyboard(keyboard);

...

}

键盘的使用方式非常简单, 通过KeyboardManager实现调用

数字键盘:

KeyboardManager keyboardManagerNumber = new KeyboardManager(this);

keyboardManagerNumber.bindToEditor(editText2, new NumberKeyboard(context,NumberKeyboard.DEFAULT_NUMBER_XML_LAYOUT));

字母键盘:

KeyboardManager keyboardManagerAbc = new KeyboardManager(this);

keyboardManagerAbc.bindToEditor(editText1, new ABCKeyboard(context, ABCKeyboard.DEFAULT_ABC_XML_LAYOUT));

至此,自定义键盘的实现就介绍完了,文中介绍的更多还是实现的思路,具体实现可以参考github,有需要的用户也可以直接修改项目的源码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值