问题
自定义车牌省份键盘KeyboardView.OnKeyboardActionListener中onPress(int primaryCode)方法,打印参数primaryCode为0
解决办法
新建KeyBoard类继承android.inputmethodservice.Keyboard,重写getNearestKeys()方法。
package com.sybil052.xxx.views;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import java.util.List;
public class KeyBoard extends Keyboard {
public KeyBoard(Context context, int xmlLayoutResId) {
super(context, xmlLayoutResId);
}
public KeyBoard(Context context, int xmlLayoutResId, int modeId, int width, int height) {
super(context, xmlLayoutResId, modeId, width, height);
}
public KeyBoard(Context context, int xmlLayoutResId, int modeId) {
super(context, xmlLayoutResId, modeId);
}
public KeyBoard(Context context, int layoutTemplateResId, CharSequence characters, int columns, int horizontalPadding) {
super(context, layoutTemplateResId, characters, columns, horizontalPadding);
}
@Override
public int[] getNearestKeys(int x, int y) {
List<Key> keys = getKeys();
Key[] mKeys = keys.toArray(new Key[keys.size()]);
int i = 0;
for (Key key : mKeys) {
if(key.isInside(x, y)) {
return new int[]{i};
}
i++;
}
return new int[0];
}
}