1. 程式人生 > >android自定義密碼鍵盤

android自定義密碼鍵盤


先看介面佈局檔案

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical">
  6.     <EditText
  7.         android:id="@+id/edit"
  8.         android:layout_width
    ="fill_parent"
  9.         android:layout_height="wrap_content"/>
  10.     <EditText
  11.         android:id="@+id/edit1"
  12.         android:layout_width="fill_parent"
  13.         android:layout_height="wrap_content"
  14.         android:password="true"/>
  15.     <RelativeLayout
  16.         android:layout_width="fill_parent"
  17.         android:layout_height
    ="wrap_content">
  18.         <android.inputmethodservice.KeyboardView
  19.             android:id="@+id/keyboard_view"
  20.             android:layout_width="fill_parent"
  21.             android:layout_height="wrap_content"
  22.             android:layout_alignParentBottom="true"
  23.             android:focusable="true"
  24.             android:focusableInTouchMode
    ="true"
  25.             android:background="@color/lightblack"
  26.             android:keyBackground="@drawable/btn_keyboard_key"
  27.             android:keyTextColor="@color/white"
  28.             android:visibility="gone"/>
  29.     </RelativeLayout>
  30. </LinearLayout>

通過佈局檔案可以看出介面上有兩個輸入框,其中一個是密碼輸入框,介面上還有一個隱藏的鍵盤控制元件。
在res下新建xml資料夾,在xml資料夾中新建qwerty.xml和symbols.xml檔案. qwerty.xml 是字母鍵盤佈局,symbols.xml 是數字鍵盤佈局,內如如下

qwerty.xml內容

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <Keyboardandroid:keyWidth="10.000002%p"android:keyHeight="@dimen/key_height"
  3.         android:horizontalGap="0.0px"android:verticalGap="0.0px"
  4.         xmlns:android="http://schemas.android.com/apk/res/android">
  5.         <Row>
  6.                 <Keyandroid:codes="113"android:keyEdgeFlags="left"
  7.                         android:keyLabel="q"/>
  8.                 <Keyandroid:codes="119"android:keyLabel="w"/>
  9.                 <Keyandroid:codes="101"android:keyLabel="e"/>
  10.                 <Keyandroid:codes="114"android:keyLabel="r"/>
  11.                 <Keyandroid:codes="116"android:keyLabel="t"/>
  12.                 <Keyandroid:codes="121"android:keyLabel="y"/>
  13.                 <Keyandroid:codes="117"android:keyLabel="u"/>
  14.                 <Keyandroid:codes="105"android:keyLabel="i"/>
  15.                 <Keyandroid:codes="111"android:keyLabel="o"/>
  16.                 <Keyandroid:codes="112"android:keyEdgeFlags="right"
  17.                         android:keyLabel="p"/>
  18.         </Row>
  19.         <Row>
  20.                 <Keyandroid:horizontalGap="4.999995%p"android:codes="97"
  21.                         android:keyEdgeFlags="left"android:keyLabel="a"/>
  22.                 <Keyandroid:codes="115"android:keyLabel="s"/>
  23.                 <Keyandroid:codes="100"android:keyLabel="d"/>
  24.                 <Keyandroid:codes="102"android:keyLabel="f"/>
  25.                 <Keyandroid:codes="103"android:keyLabel="g"/>
  26.                 <Keyandroid:codes="104"android:keyLabel="h"/>
  27.                 <Keyandroid:codes="106"android:keyLabel="j"/>
  28.                 <Keyandroid:codes="107"android:keyLabel="k"/>
  29.                 <Keyandroid:codes="108"android:keyEdgeFlags="right"
  30.                         android:keyLabel="l"/>
  31.         </Row>
  32.         <Row>
  33.                 <Keyandroid:keyWidth="14.999998%p"android:codes="-1"
  34.                         android:keyEdgeFlags="left"android:isModifier="true"
  35.                         android:isSticky="true"android:keyIcon="@drawable/sym_keyboard_shift"/>
  36.                 <Keyandroid:codes="122"android:keyLabel="z"/>
  37.                 <Keyandroid:codes="120"android:keyLabel="x"/>
  38.                 <Keyandroid:codes="99"android:keyLabel="c"/>
  39.                 <Keyandroid:codes="118"android:keyLabel="v"/>
  40.                 <Keyandroid:codes="98"android:keyLabel="b"/>
  41.                 <Keyandroid:codes="110"android:keyLabel="n"/>
  42.                 <Keyandroid:codes="109"android:keyLabel="m"/>
  43.                 <Keyandroid:keyWidth="14.999998%p"android:codes="-5"
  44.                         android:keyEdgeFlags="right"android:isRepeatable="true"
  45.                         android:keyIcon="@drawable/sym_keyboard_delete"/>
  46.         </Row>
  47.         <Rowandroid:rowEdgeFlags="bottom">
  48.                 <Keyandroid:keyWidth="20.000004%p"android:codes="-2"
  49.                         android:keyLabel="12#"/>
  50.                 <Keyandroid:keyWidth="14.999998%p"android:codes="44"
  51.                         android:keyLabel=","/>
  52.                 <Keyandroid:keyWidth="29.999996%p"android:codes="32"
  53.                         android:isRepeatable="true"android:keyIcon="@drawable/sym_keyboard_space"/>
  54.                 <Keyandroid:keyWidth="14.999998%p"android:codes="46"
  55.                         android:keyLabel="."/>
  56.                 <Keyandroid:keyWidth="20.000004%p"android:codes="-3"
  57.                         android:keyEdgeFlags="right"android:keyLabel="完成"/>
  58.         </Row>
  59. </Keyboard>

symbols.xml 內容
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <Keyboardxmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:keyWidth="25%p"android:horizontalGap="0px"
  4.         android:verticalGap="0px"android:keyHeight="@dimen/key_height">
  5.         <Row>
  6.                 <Keyandroid:codes="49"android:keyLabel="1"/>
  7.                 <Keyandroid:codes="50"android:keyLabel="2"/>
  8.                 <Keyandroid:codes="51"android:keyLabel="3"/>
  9.                 <Keyandroid:codes="57419"android:keyEdgeFlags="right"
  10.                         android:keyIcon="@drawable/sym_keyboard_left"/>
  11.         </Row>
  12.         <Row>
  13.                 <Keyandroid:codes="52"android:keyLabel="4"/>
  14.                 <Keyandroid:codes="53"android:keyLabel="5"/>
  15.                 <Keyandroid:codes="54"android:keyLabel="6"/>
  16.                 <Keyandroid:codes="57421"android:keyEdgeFlags="right"
  17.                         android:keyIcon="@drawable/sym_keyboard_right"/>
  18.         </Row>
  19.         <Row>
  20.                 <Keyandroid:codes="55"android:keyLabel="7"/>
  21.                 <Keyandroid:codes="56"android:keyLabel="8"/>
  22.                 <Keyandroid:codes="57"android:keyLabel="9"/>
  23.                 <Keyandroid:codes="-3"android:keyHeight="100dip"
  24.                         android:keyEdgeFlags="right"android:isRepeatable="true"
  25.                         android:keyLabel="完成"/>
  26.         </Row>
  27.         <Row>
  28.                 <Keyandroid:codes="-2"android:keyLabel="ABC"/>
  29.                 <Keyandroid:codes="48"android:keyLabel="0"/>
  30.                 <Keyandroid:codes="-5"android:keyIcon="@drawable/sym_keyboard_delete"/>
  31.         </Row>
  32. </Keyboard>

KeydemoActivity.java
  1. package cn.key;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.os.Bundle;  
  5. import android.text.InputType;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.View.OnTouchListener;  
  9. import android.widget.EditText;  
  10. publicclass KeydemoActivity extends Activity {  
  11.         private Context ctx;  
  12.         private Activity act;  
  13.         private EditText edit;  
  14.         private EditText edit1;  
  15.         @Override
  16.         publicvoid onCreate(Bundle savedInstanceState) {  
  17.                 super.onCreate(savedInstanceState);  
  18.                 setContentView(R.layout.main);  
  19.                 ctx = this;  
  20.                 act = this;  
  21.                 edit = (EditText) this.findViewById(R.id.edit);  
  22.                 edit.setInputType(InputType.TYPE_NULL);  
  23.                 edit1 = (EditText) this.findViewById(R.id.edit1);  
  24.                 edit.setOnTouchListener(new OnTouchListener() {  
  25.                         @Override
  26.                         publicboolean onTouch(View v, MotionEvent event) {  
  27.                                 new KeyboardUtil(act, ctx, edit).showKeyboard();  
  28.                                 returnfalse;  
  29.                         }  
  30.                 });  
  31.                 edit1.setOnTouchListener(new OnTouchListener() {  
  32.                         @Override
  33.                         publicboolean onTouch(View v, MotionEvent event) {  
  34.                                 int inputback = edit1.getInputType();  
  35.                                 edit1.setInputType(InputType.TYPE_NULL);  
  36.                                 new KeyboardUtil(act, ctx, edit1).showKeyboard();  
  37.                                 edit1.setInputType(inputback);  
  38.                                 returnfalse;  
  39.                         }  
  40.                 });  
  41.         }  
  42. }  

KeyboardUtil.java
  1. package cn.key;  
  2. import java.util.List;  
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.inputmethodservice.Keyboard;  
  6. import android.inputmethodservice.KeyboardView;  
  7. import android.inputmethodservice.Keyboard.Key;  
  8. import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;  
  9. import android.text.Editable;  
  10. import android.view.View;  
  11. import android.widget.EditText;  
  12. publicclass KeyboardUtil {  
  13.         private Context ctx;  
  14.         private Activity act;  
  15.         private KeyboardView keyboardView;  
  16.         private Keyboard k1;// 字母鍵盤
  17.         private Keyboard k2;// 數字鍵盤
  18.         publicboolean isnun = false;// 是否資料鍵盤
  19.         publicboolean isupper = false;// 是否大寫
  20.         private EditText ed;  
  21.         public KeyboardUtil(Activity act, Context ctx, EditText edit) {  
  22.                 this.act = act;  
  23.                 this.ctx = ctx;  
  24.                 this.ed = edit;  
  25.                 k1 = new Keyboard(ctx, R.xml.qwerty);  
  26.                 k2 = new Keyboard(ctx, R.xml.symbols);  
  27.                 keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);  
  28.                 keyboardView.setKeyboard(k1);  
  29.                 keyboardView.setEnabled(true);  
  30.                 keyboardView.setPreviewEnabled(true);  
  31.                 keyboardView.setOnKeyboardActionListener(listener);  
  32.         }  
  33.         private OnKeyboardActionListener listener = new OnKeyboardActionListener() {  
  34.                 @Override
  35.                 publicvoid swipeUp() {  
  36.                 }  
  37.                 @Override
  38.                 publicvoid swipeRight() {  
  39.                 }  
  40.                 @Override
  41.                 publicvoid swipeLeft() {  
  42.                 }  
  43.                 @Override
  44.                 publicvoid swipeDown() {  
  45.                 }  
  46.                 @Override
  47.                 publicvoid onText(CharSequence text) {  
  48.                 }  
  49.                 @Override
  50.                 publicvoid onRelease(int primaryCode) {  
  51.                 }  
  52.                 @Override
  53.                 publicvoid onPress(int primaryCode) {  
  54.                 }  
  55.                 @Override
  56.                 publicvoid onKey(int primaryCode, int[] keyCodes) {  
  57.                         Editable editable = ed.getText();  
  58.                         int start = ed.getSelectionStart();  
  59.                         if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
  60.                                 hideKeyboard();  
  61.                         } elseif (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
  62.                                 if (editable != null && editable.length() > 0) {  
  63.                                         if (start > 0) {  
  64.                                                 editable.delete(start - 1, start);  
  65.                                         }  
  66.                                 }  
  67.                         } elseif (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小寫切換
  68.                                 changeKey();  
  69.                                 keyboardView.setKeyboard(k1);  
  70.                         } elseif (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 數字鍵盤切換
  71.                                 if (isnun) {  
  72.                                         isnun = false;  
  73.                                         keyboardView.setKeyboard(k1);  
  74.                                 } else {  
  75.                                         isnun = true;  
  76.                                         keyboardView.setKeyboard(k2);  
  77.                                 }  
  78.                         } elseif (primaryCode == 57419) { // go left
  79.                                 if (start > 0) {  
  80.                                         ed.setSelection(start - 1);  
  81.                                 }  
  82.                         } elseif (primaryCode == 57421) { // go right
  83.                                 if (start < ed.length()) {  
  84.                                         ed.setSelection(start + 1);  
  85.                                 }  
  86.                         } else {  
  87.                                 editable.insert(start, Character.toString((char) primaryCode));  
  88.                         }  
  89.                 }  
  90.         };  
  91.         /** 
  92.          * 鍵盤大小寫切換 
  93.          */
  94.         privatevoid changeKey() {  
  95.                 List<Key> keylist = k1.getKeys();  
  96.                 if (isupper) {//大寫切換小寫
  97.                         isupper = false;  
  98.                         for(Key key:keylist){  
  99.                                 if (key.label!=null && isword(key.label.toString())) {  
  100.                                         key.label = key.label.toString().toLowerCase();  
  101.                                         key.codes[0] = key.codes[0]+32;  
  102.                                 }  
  103.                         }  
  104.                 } else {//小寫切換大寫
  105.                         isupper = true;  
  106.                         for(Key key:keylist){  
  107.                                 if (key.label!=null && isword(key.label.toString())) {  
  108.                                         key.label = key.label.toString().toUpperCase();  
  109.                                         key.codes[0] = key.codes[0]-32;  
  110.                                 }  
  111.                         }  
  112.                 }  
  113.         }  
  114.     publicvoid showKeyboard() {  
  115.         int visibility = keyboardView.getVisibility();  
  116.         if (visibility == View.GONE || visibility == View.INVISIBLE) {  
  117.             keyboardView.setVisibility(View.VISIBLE);  
  118.         }  
  119.     }  
  120.     publicvoid hideKeyboard() {  
  121.         int visibility = keyboardView.getVisibility();  
  122.         if (visibility == View.VISIBLE) {  
  123.             keyboardView.setVisibility(View.INVISIBLE);  
  124.         }  
  125.     }  
  126.     privateboolean isword(String str){  
  127.             String wordstr = "abcdefghijklmnopqrstuvwxyz";  
  128.             if (wordstr.indexOf(str.toLowerCase())>-1) {  
  129.                         returntrue;  
  130.                 }  
  131.             returnfalse;  
  132.     }  
}