在res下建立xml包,在xml包内建立keyboard.xml文件:
1 <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" 2 android:keyHeight="50dp" 3 android:keyWidth="%25p" 4 android:horizontalGap="1dp" 5 android:verticalGap="1dp"> 6 <!-- keyHeight每一个按键的高度,keyWidth每一个按键的宽度,horizontalGap水平分割线 --> 7 <Row> 8 <Key android:codes="49" android:keyLabel="1"/> 9 <Key android:codes="50" android:keyLabel="2"/> 10 <Key android:codes="51" android:keyLabel="3"/> 11 <Key android:codes="-5" android:keyLabel="删除"/> 12 </Row> 13 <!-- codes是ascii码,然后删除键是特定的-5 --> 14 <Row> 15 <Key android:codes="52" android:keyLabel="4"/> 16 <Key android:codes="53" android:keyLabel="5"/> 17 <Key android:codes="54" android:keyLabel="6"/> 18 <Key android:codes="-4" android:keyHeight="150dp" android:keyLabel="确定"/> 19 </Row> 20 <Row> 21 <Key android:codes="55" android:keyLabel="7"/> 22 <Key android:codes="56" android:keyLabel="8"/> 23 <Key android:codes="57" android:keyLabel="9"/> 24 </Row> 25 <Row> 26 <Key android:codes="-3" android:keyLabel="清零"/> 27 <Key android:codes="48" android:keyLabel="0"/> 28 <Key android:codes="46" android:keyLabel="."/> 29 </Row> 30 </Keyboard>
然后又建立了一个utils包,在该包下建立keyBoardUtils.java文件:
1 public class keyBoardUtils { 2 private final Keyboard k1; 3 private KeyboardView keyboardView; 4 private EditText editText; 5 6 public interface OnEnsureListener{ 7 public void OnEnsure(); 8 } 9 OnEnsureListener onEnsureListener; 10 public void setOnEnsureListener(OnEnsureListener onEnsureListener){ 11 this.onEnsureListener=onEnsureListener; 12 } 13 14 public keyBoardUtils(KeyboardView keyboardView, EditText editText) { 15 this.keyboardView = keyboardView; 16 this.editText = editText; 17 this.editText.setInputType(InputType.TYPE_NULL);//取消弹出系统键盘 18 k1=new Keyboard(this.editText.getContext(), R.xml.keyboard); 19 this.keyboardView.setKeyboard(k1); 20 this.keyboardView.setEnabled(true); 21 this.keyboardView.setPreviewEnabled(false);//禁止弹出 22 this.keyboardView.setOnKeyboardActionListener(listener);//设置键盘按钮点击监听 23 } 24 KeyboardView.OnKeyboardActionListener listener=new KeyboardView.OnKeyboardActionListener() { 25 @Override 26 public void onPress(int i) { 27 28 } 29 30 @Override 31 public void onRelease(int i) { 32 33 } 34 @Override 35 public void onKey(int i, int[] ints) { 36 Editable editable = editText.getText(); 37 int start=editText.getSelectionStart(); 38 switch (i){ 39 case Keyboard.KEYCODE_DELETE://点击了删除 40 if(editable!=null&&editable.length()>0){ 41 if(start>0){ 42 editable.delete(start-1,start); 43 } 44 } 45 break; 46 case Keyboard.KEYCODE_CANCEL://点击了清零 47 editable.clear(); 48 break; 49 case Keyboard.KEYCODE_DONE://点击了确定 50 onEnsureListener.OnEnsure();//通过接口回调的方法,点击确定调用该方法 51 break; 52 default://其他的数字直接插入 53 editable.insert(start,Character.toString((char)i)); 54 break; 55 } 56 } 57 @Override 58 public void onText(CharSequence charSequence) { } 59 @Override 60 public void swipeLeft() { } 61 @Override 62 public void swipeRight() { } 63 @Override 64 public void swipeDown() { } 65 @Override 66 public void swipeUp() { } 67 }; 68 69 public void showKewboard(){ 70 int visibility=keyboardView.getVisibility(); 71 if(visibility==View.INVISIBLE||visibility==View.GONE){ 72 keyboardView.setVisibility(View.VISIBLE); 73 } 74 }//显示键盘 75 76 public void hideKeyboard(){ 77 int visibility=keyboardView.getVisibility(); 78 if(visibility==View.VISIBLE||visibility==View.INVISIBLE){ 79 keyboardView.setVisibility(View.GONE); 80 } 81 }//隐藏键盘 82 }