6.17 5
自定义软键盘
package com.zhen.accountbook.utils; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.text.Editable; import android.text.InputType; import android.view.View; import android.widget.EditText; import com.zhen.accountbook.R; public class KeyBoardUtils { private final Keyboard k1; private KeyboardView keyboardView; private EditText editText; public interface OnEnsureListener { public void onEnsure(); } OnEnsureListener onEnsureListener; public void setOnEnsureListener(OnEnsureListener onEnsureListener) { this.onEnsureListener = onEnsureListener; } public KeyBoardUtils(KeyboardView keyboardView, EditText editText) { this.keyboardView = keyboardView; this.editText = editText; this.editText.setInputType(InputType.TYPE_NULL);//取消弹出系统键盘 k1 = new Keyboard(this.editText.getContext(), R.xml.key); this.keyboardView.setKeyboard(k1);//设置要显示键盘的样式 this.keyboardView.setEnabled(true); this.keyboardView.setPreviewEnabled(false); this.keyboardView.setOnKeyboardActionListener(listener);//设置键盘按钮被点击的监听 } KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() { @Override public void onPress(int i) { } @Override public void onRelease(int i) { } @Override public void onKey(int i, int[] ints) { Editable editable = editText.getText(); int start = editText.getSelectionStart(); switch (i) { case Keyboard.KEYCODE_DELETE: if (editable != null && editable.length() > 0) { if (start > 0) { editable.delete(start - 1, start); } } break;//点击了删除键 case Keyboard.KEYCODE_CANCEL: editable.clear(); break;//点击了清零 case Keyboard.KEYCODE_DONE: onEnsureListener.onEnsure();//通过接口回调的方法,当点击确定时,可以调用这个方法 break;//点击了完成 default://其他的数字直接插入 editable.insert(start, Character.toString((char) i)); break; } } @Override public void onText(CharSequence charSequence) { } @Override public void swipeLeft() { } @Override public void swipeRight() { } @Override public void swipeDown() { } @Override public void swipeUp() { } }; //显示键盘的方法 public void showKeyBoard() { int visibility = keyboardView.getVisibility(); if (visibility == View.INVISIBLE || visibility == View.GONE) { keyboardView.setVisibility(View.VISIBLE); } } //隐藏键盘的方法 public void hideKeyBoard() { int visibility = keyboardView.getVisibility(); if (visibility == View.INVISIBLE || visibility == View.VISIBLE) { keyboardView.setVisibility(View.GONE); } } }
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyHeight="50dp" android:keyWidth="25%p" android:horizontalGap="1px" android:verticalGap="1px"> <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="-5" android:keyLabel="删除" /> </Row> <Row> <Key android:codes="52" android:keyLabel="4" /> <Key android:codes="53" android:keyLabel="5" /> <Key android:codes="54" android:keyLabel="6" /> <Key android:codes="-4" keyHeight="150dp" android:keyLabel="确定" />" </Row> <Row> <Key android:codes="55" android:keyLabel="7" /> <Key android:codes="56" android:keyLabel="8" /> <Key android:codes="57" android:keyLabel="9" /> </Row> <Row> <Key android:codes="-3" android:keyLabel="清零" /> <Key android:codes="48" android:keyLabel="0" /> <Key android:codes="46" android:keyLabel="." /> </Row> </Keyboard>