48_自定义键盘实现

 
#1 效果图(多种键盘视图,请看完gif)
 
ss
ss
ss
#2 步骤(只显示关键代码,具体实现在示例代码中)
1.屏蔽系统键盘
@Override
public void onCreate(Bundle savedInstanceState) {
...
if (android.os.Build.VERSION.SDK_INT <= 10) { //解决sdk4.0以上光标不显示问题
etzqdm.setInputType(InputType.TYPE_NULL);
} else {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
try {
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(etzqdm, false);
} catch (Exception e) {
e.printStackTrace();
}
}
...
}

 

2. 配置KeyBoard键盘
1)xml中声明
 
 
2)res中新建xml文件夹,配置键盘视图(相关xml属性见个人前一篇博客)
 
一个简单的数字键盘
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="0dp"
android:keyHeight="10%p"
android:keyWidth="25%p"
android:verticalGap="0dp">
<Row>
<Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left" />
<Key android:codes="50" android:keyLabel="2" />
<Key android:codes="51" android:keyLabel="3" />
<Key
android:codes="-5"
android:keyHeight="20%p"
android:keyEdgeFlags="right"
android:keyIcon="@drawable/keyboard_delete"
/>
</Row>
<Row>
<Key android:codes="52" android:keyLabel="4" android:keyEdgeFlags="left" />
<Key android:codes="53" android:keyLabel="5" />
<Key android:codes="54" android:keyLabel="6" />
</Row>
<Row>
<Key android:codes="55" android:keyLabel="7" android:keyEdgeFlags="left" />
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="57" android:keyLabel="9" />
<Key
android:codes="-4"
android:keyHeight="20%p"
android:keyEdgeFlags="right"
android:keyLabel="买入"
/>
</Row>
<Row>
<Key android:codes="46" android:keyLabel="." android:keyEdgeFlags="left" />
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="-3" android:keyLabel="收起" />
</Row>
</Keyboard>

 

 
 
3.代码中初始化键盘
private void initKeyboard() {

mKeyboardView.setKeyboard(new Keyboard(this, R.xml.keyboard_number));
mKeyboardView.setEnabled(true);
mKeyboardView.setPreviewEnabled(true);
mKeyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = et.getText();//et为EditText
int start = et.getSelectionStart();
switch (primaryCode) {
case Keyboard.KEYCODE_DELETE:
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
break;
case Keyboard.KEYCODE_DONE:
hideKeyboard();
Toast.makeText(NumberboardActivity.this,"买入",Toast.LENGTH_SHORT).show();
break;
case Keyboard.KEYCODE_CANCEL:
hideKeyboard();
break;
default:
String str = Character.toString((char) primaryCode);
editable.insert(start, str);
break;
}
}

public void onPress(int primaryCode) {
}

public void onRelease(int primaryCode) {
}

public void onText(CharSequence text) {
}

public void swipeDown() {
}

public void swipeLeft() {
}

public void swipeRight() {
}

public void swipeUp() {
}
});
}

 

 
4. 提供键盘隐藏显示方法,EditText调用
 
private void hideKeyboard() {
int visibility = mKeyboardView.getVisibility();
if (visibility == View.VISIBLE) {
mKeyboardView.setVisibility(View.GONE);
}
}

private void showKeyboard() {
int visibility = mKeyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
mKeyboardView.setVisibility(View.VISIBLE);
}
}

...
et.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
showKeyboard();
}
});
...

 

#3 示例代码
http://download.csdn.net/detail/baopengjian/9817320
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值