今天早上上班,看见有个技术群里面在讨论自定义软件盘的问题,我之前也没有接触过,所以在这里学习一下,做个记录,
软件盘我们自己定义的简洁方法
- 在Android中,Android 提供了一个KeyboardView 类,这是用于处理绘制键盘、检测按键和触摸动作的类
Keyboard ,监听虚拟按键的类 ,
有了这两个类,使用它的属性就可以进行自定义软件盘了
一、简单的设置
keyboardView =(KeyboardView)findViewById(R.id.keyboard_view);
keyboardView.setKeyboard(new Keyboard(this, R.xml.xxx)); //设置监听键盘的类
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(true);
keyboardView.setOnKeyboardActionListener(listener);
二、数字键盘的使用
这个键盘的内容是需要我们在xml文件中定义的,名称当然是按你想的去命名,但是格式是固定的,接下来我们在res 下建立一个xml文件夹,将我们的要使用的xxx.xml建好,用keyboard做根标签
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="20%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="@dimen/key_height">
<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="52" android:keyLabel="4" />
<Key android:codes="-5" android:keyIcon="@drawable/keyboard_delete" />
</Row>
<Row>
<Key android:codes="53" android:keyLabel="5" />
<Key android:codes="54" android:keyLabel="6" />
<Key android:codes="55" android:keyLabel="7" />
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="-2" android:keyLabel="中文" />
</Row>
<Row>
<Key android:codes="57" android:keyLabel="9" />
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="46" android:keyLabel="." />
<Key android:codes="-3" android:keyWidth="40%p"
android:isRepeatable="true" android:keyLabel="完成" />
</Row>
</Keyboard>
keyboard 说明的是一个软件盘定义文件,Row说明这是一行按键的定义,Key 说明这就是一个按键元素,
codes 按键对应的输出值,它可以是unicode 值 、逗号分割的多个值,也可以是字符串,
属性 | 说明 |
---|---|
keyLabel | 按键显示的内容 |
keyIcon | 按键显示的图标内容 |
keyWidth | 按键的宽度 |
keyHeight | 按键的高度 |
horizontalGap | 代表按键前的间隙水平方向上的 |
isSticky | 按键是否是sticky的,就像shift 键 具有两种状态 |
isModifier | 按键是不是功能键 |
keyOutputText | 指定按键输出的内容是字符串 |
isRepeatable | 按键是可重复的,如果长按键可以触发重复按键事件则为true,else为false |
keyEdgeFlags | 指定按键的对齐指令,取值为left或者right |
当然,我们在设置每一个按键codes的时候,都是按照keyboard 类中定义的属性去完成的,
public static final int EDGE_LEFT = 1;
public static final int EDGE_RIGHT = 2;
public static final int EDGE_TOP = 4;
public static final int EDGE_BOTTOM = 8;
public static final int KEYCODE_SHIFT = -1;
public static final int KEYCODE_MODE_CHANGE = -2;
public static final int KEYCODE_CANCEL = -3;
public static final int KEYCODE_DONE = -4;
public static final int KEYCODE_DELETE = -5;
public static final int KEYCODE_ALT = -6;
上面这是一个数字键盘
当然我们在操作的时候就可以这样使用了
比如
<key android:codes="-1"></key>
Java 代码中的使用就是
if(primaryCode == KEYCODE_SHIFT) //这就是按了shift键了
具体就可以在里面添加逻辑了
三、实现键盘动作监听OnKeyboardActionListener
private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
@Override
public void swipeUp() { //上滑动
}
@Override
public void swipeRight() {//右滑动
}
@Override
public void swipeLeft() {//左滑动
}
@Override
public void swipeDown() { //下滑动
}
@Override
public void onText(CharSequence text) {
}
@Override
public void onRelease(int primaryCode) { //松开触发
}
@Override
public void onPress(int primaryCode) {//按下触发
}
@Override
public void onKey(int primaryCode, int[] keyCodes) { //松开出发 在onRelease之前触发
Editable editable = ed.getText();
int start = ed.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
changeKey();
keyboardView.setKeyboard(k1);
} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
if (isnun) {
isnun = false;
keyboardView.setKeyboard(k1);
} else {
isnun = true;
keyboardView.setKeyboard(k2);
}
} else if (primaryCode == 57419) { // go left
if (start > 0) {
ed.setSelection(start - 1);
}
} else if (primaryCode == 57421) { // go right
if (start < ed.length()) {
ed.setSelection(start + 1);
}
} else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
};
/**
* 键盘大小写切换
*/
private void changeKey() {
List<Key> keylist = k1.getKeys();
if (isupper) {//大写切换小写
isupper = false;
for(Key key:keylist){
if (key.label!=null && isword(key.label.toString())) {
key.label = key.label.toString().toLowerCase();
key.codes[0] = key.codes[0]+32;
}
}
} else {//小写切换大写
isupper = true;
for(Key key:keylist){
if (key.label!=null && isword(key.label.toString())) {
key.label = key.label.toString().toUpperCase();
key.codes[0] = key.codes[0]-32;
}
}
}
}
//显示
public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
keyboardView.setVisibility(View.VISIBLE);
}
}
//隐藏
public void hideKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE) {
keyboardView.setVisibility(View.INVISIBLE);
}
}
/**
*是否是单词
**/
private boolean isword(String str){
String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase())>-1) {
return true;
}
return false;
}
四、字母键盘的XML 其他的使用和上面一样
<?xml version="1.0" encoding="utf-8"?>
<Keyboard android:keyWidth="10.000002%p" android:keyHeight="@dimen/key_height"
android:horizontalGap="0.0px" android:verticalGap="0.0px"
xmlns:android="http://schemas.android.com/apk/res/android">
<Row>
<Key android:codes="113" android:keyEdgeFlags="left"
android:keyLabel="q" />
<Key android:codes="119" android:keyLabel="w" />
<Key android:codes="101" android:keyLabel="e" />
<Key android:codes="114" android:keyLabel="r" />
<Key android:codes="116" android:keyLabel="t" />
<Key android:codes="121" android:keyLabel="y" />
<Key android:codes="117" android:keyLabel="u" />
<Key android:codes="105" android:keyLabel="i" />
<Key android:codes="111" android:keyLabel="o" />
<Key android:codes="112" android:keyEdgeFlags="right"
android:keyLabel="p" />
</Row>
<Row>
<Key android:horizontalGap="4.999995%p" android:codes="97"
android:keyEdgeFlags="left" android:keyLabel="a" />
<Key android:codes="115" android:keyLabel="s" />
<Key android:codes="100" android:keyLabel="d" />
<Key android:codes="102" android:keyLabel="f" />
<Key android:codes="103" android:keyLabel="g" />
<Key android:codes="104" android:keyLabel="h" />
<Key android:codes="106" android:keyLabel="j" />
<Key android:codes="107" android:keyLabel="k" />
<Key android:codes="108" android:keyEdgeFlags="right"
android:keyLabel="l" />
</Row>
<Row>
<Key android:keyWidth="14.999998%p" android:codes="-1"
android:keyEdgeFlags="left" android:isModifier="true"
android:isSticky="true" android:keyIcon="@drawable/sym_keyboard_shift" />
<Key android:codes="122" android:keyLabel="z" />
<Key android:codes="120" android:keyLabel="x" />
<Key android:codes="99" android:keyLabel="c" />
<Key android:codes="118" android:keyLabel="v" />
<Key android:codes="98" android:keyLabel="b" />
<Key android:codes="110" android:keyLabel="n" />
<Key android:codes="109" android:keyLabel="m" />
<Key android:keyWidth="14.999998%p" android:codes="-5"
android:keyEdgeFlags="right" android:isRepeatable="true"
android:keyIcon="@drawable/sym_keyboard_delete" />
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:keyWidth="20.000004%p" android:codes="-2"
android:keyLabel="12#" />
<Key android:keyWidth="14.999998%p" android:codes="44"
android:keyLabel="," />
<Key android:keyWidth="29.999996%p" android:codes="32"
android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />
<Key android:keyWidth="14.999998%p" android:codes="46"
android:keyLabel="." />
<Key android:keyWidth="20.000004%p" android:codes="-3"
android:keyEdgeFlags="right" android:keyLabel="完成" />
</Row>
</Keyboard>
五、汉字
当然这因该是我们比较关心的,但是说实话这也是最麻烦的,因为我们的汉子太多了,要是以上边的方法写,还真的不是一个小kiss,
诺,在这儿呢,汉子的编码表,牛逼你去写 http://blog.csdn.net/lintax/article/details/51866861 哈哈