全键盘android,自定义全键盘-[Android_YangKe]

尊重劳动者创作,转发请注明出处

首先看本文实现的效果。

325a7efd5d0d?mType=Group

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="http://schemas.android.com/apk/res/android">

再坚持一下,再努力一下,再前进一下,也许,成功离你只有一步之遥。有句话说,哪怕只有百分之一的希望,我也要付出百分之百的努力。学习不能停!!

** ps: 有帮助的话: 喜欢、评论、转发,动一动你的小手让更多的人知道!关注 帅比-杨**

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值