1. 程式人生 > 其它 >安卓自定義軟鍵盤

安卓自定義軟鍵盤

在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 }