尊重劳动者创作,转发请注明出处
首先看本文实现的效果。
yangke.gif
学而不思则罔,思而不学则殆。一眨眼五一小长假就结束了,完全没玩嗨呢,不能完全进入工作状态,小伙伴们扶我起来嗨。一年嗨两次,一次嗨半年。好了,言归正传,今天是一个自定义键盘。
首先了解 API
KeyboardView
Keyboard
KeyboardView
KeyboardView 一个用于呈现虚拟键盘的 view,同时处理着每一个键盘所对应的点击、触摸等事件。
常用属性:
XML attributes
Layout resource for key press feedback.
Vertical offset of the key press feedback from the key.
Color to use for the label in a key.
Size of the text for character keys.
Size of the text for custom keys with some text and no icon.
Layout resource for popup keyboards.
Amount to offset the touch Y coordinate by, for bias correction.
顾名思义这些 API 很简单,这里不做过多的解释。在使用时我们需要特别注意下 android:verticalCorrection 。由于 android 设备众多,键盘 y 轴触摸区域可能存在偏差,此 API 是用于矫正偏移的 Y 坐标。具体作用自己可以在 xml 手动更改进行感受。
Keyboard
Keyboard 的样式是以 XML 文件的形式存在的,由多个 Row 和 Key 组成,我们可以直接在 XML 定义键盘的行、键、以及键大小,下面看代码块。
android:keyWidth="%10p"
android:keyHeight="50px"
android:horizontalGap="2px"
android:verticalGap="2px" >
...
...
其中 Keyboard 表示整个键盘, Row 表示其中一行,Key 表示某一个具体按键。也就是说键盘的大小,样式都可以在这个文件中进行定义,下面通过一个表格对 Keyboard 必要属性进行简单了解。
XML attributes
Default horizontal gap between keys.
Default height of a key, in pixels or percentage of display width.
Default width of a key, in pixels or percentage of display width.
Default vertical gap between rows of keys.
OK,到这里自定义键盘所需要了解的知识已经足够了,下面进入编码模式。
public class KeyboardViewS implements OnKeyboardActionListener {
private static final int KEYBOARD_DURATION = 350;
private EditText mEditText;
private Keyboard mKeyboard;
/** 是否大写 */
private boolean isUperCase = false;
private android.inputmethodservice.KeyboardView keyboardView;
public KeyboardViewS(Activity act, EditText edit) {
this.mEditText = edit;
mKeyboard = new Keyboard(act, R.xml.qwerty);
keyboardView = (android.inputmethodservice.KeyboardView) (act.findViewById(R.id.keyboard_view));
keyboardView.setKeyboard(mKeyboard);
/** 设置没有弹窗的提示 */
keyboardView.setPreviewEnabled(false);
keyboardView.setOnKeyboardActionListener(this);
uperCase();
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = mEditText.getText();
int start = mEditText.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) { // 完成
hideKeyboard(keyboardView);
} 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) {// 大小写切换
uperCase();
keyboardView.setKeyboard(mKeyboard);
} else if (primaryCode == 57419) {
if (start > 0) {
mEditText.setSelection(start - 1);
}
} else if (primaryCode == 57421) {
if (start < mEditText.length()) {
mEditText.setSelection(start + 1);
}
} else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
/** 键盘大小写切换 */
private void uperCase() {
final List keylist = mKeyboard.getKeys();
if (isUperCase) {
isUperCase = 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 {
isUperCase = 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 static void setSystemInputGone(EditText editText) {
if (Build.VERSION.SDK_INT >= 11) {
Class cls = EditText.class;
try {
Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setShowSoftInputOnFocus.setAccessible(false);
setShowSoftInputOnFocus.invoke(editText, false);
} catch (Exception e) {
e.printStackTrace();
}
} else {
editText.setInputType(android.text.InputType.TYPE_NULL);
editText.setInputType(editText.getInputType());
}
}
/** 显示键盘 */
public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
Animation anim = AnimationUtils.translateAnimationOut(KEYBOARD_DURATION);
keyboardView.setVisibility(View.VISIBLE);
keyboardView.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
super.onAnimationEnd(animation);
keyboardView.clearAnimation();
}
});
}
}
/** 隐藏键盘 */
public static void hideKeyboard(final KeyboardView keyboardView) {
if (keyboardView == null) {
return;
}
if (keyboardView.getVisibility() == View.VISIBLE) {
Animation anim = AnimationUtils.translateAnimationIn(KEYBOARD_DURATION);
keyboardView.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
super.onAnimationEnd(animation);
keyboardView.clearAnimation();
}
});
keyboardView.setVisibility(View.GONE);
}
}
private boolean isword(String str) {
final String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase()) > -1) {
return true;
}
return false;
}
...
}
核心代码。尊重劳动者成功,转发请注明出处。
android:horizontalGap="5dp" android:verticalGap="3dp"
xmlns:android="/apk/res/android">
再坚持一下,再努力一下,再前进一下,也许,成功离你只有一步之遥。有句话说,哪怕只有百分之一的希望,我也要付出百分之百的努力。学习不能停!!
** ps: 有帮助的话: 喜欢、评论、转发,动一动你的小手让更多的人知道!关注 帅比-杨**