Android自定义软键盘的实现

转载:http://blog.csdn.net/hfsu0419/article/details/7924673


先看界面布局文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.    
  7.     <EditText  
  8.         android:id="@+id/edit"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content" />  
  11.    
  12.     <EditText  
  13.         android:id="@+id/edit1"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:password="true" />  
  17.    
  18.     <RelativeLayout  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content" >  
  21.    
  22.         <android.inputmethodservice.KeyboardView  
  23.             android:id="@+id/keyboard_view"  
  24.             android:layout_width="fill_parent"  
  25.             android:layout_height="wrap_content"  
  26.             android:layout_alignParentBottom="true"  
  27.             android:focusable="true"  
  28.             android:focusableInTouchMode="true"  
  29.             android:background="@color/lightblack"  
  30.             android:keyBackground="@drawable/btn_keyboard_key"  
  31.             android:keyTextColor="@color/white"  
  32.             android:visibility="gone" />  
  33.     </RelativeLayout>  
  34.    
  35. </LinearLayout>  

通过布局文件可以看出界面上有两个输入框,其中一个是密码输入框,界面上还有一个隐藏的键盘控件。
在res下新建xml文件夹,在xml文件夹中新建qwerty.xml和symbols.xml文件. qwerty.xml 是字母键盘布局,symbols.xml 是数字键盘布局,内如如下

qwerty.xml内容

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Keyboard android: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.                 <Key android:codes="113" android:keyEdgeFlags="left"  
  7.                         android:keyLabel="q" />  
  8.                 <Key android:codes="119" android:keyLabel="w" />  
  9.                 <Key android:codes="101" android:keyLabel="e" />  
  10.                 <Key android:codes="114" android:keyLabel="r" />  
  11.                 <Key android:codes="116" android:keyLabel="t" />  
  12.                 <Key android:codes="121" android:keyLabel="y" />  
  13.                 <Key android:codes="117" android:keyLabel="u" />  
  14.                 <Key android:codes="105" android:keyLabel="i" />  
  15.                 <Key android:codes="111" android:keyLabel="o" />  
  16.                 <Key android:codes="112" android:keyEdgeFlags="right"  
  17.                         android:keyLabel="p" />  
  18.         </Row>  
  19.         <Row>  
  20.                 <Key android:horizontalGap="4.999995%p" android:codes="97"  
  21.                         android:keyEdgeFlags="left" android:keyLabel="a" />  
  22.                 <Key android:codes="115" android:keyLabel="s" />  
  23.                 <Key android:codes="100" android:keyLabel="d" />  
  24.                 <Key android:codes="102" android:keyLabel="f" />  
  25.                 <Key android:codes="103" android:keyLabel="g" />  
  26.                 <Key android:codes="104" android:keyLabel="h" />  
  27.                 <Key android:codes="106" android:keyLabel="j" />  
  28.                 <Key android:codes="107" android:keyLabel="k" />  
  29.                 <Key android:codes="108" android:keyEdgeFlags="right"  
  30.                         android:keyLabel="l" />  
  31.         </Row>  
  32.         <Row>  
  33.                 <Key android: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.                 <Key android:codes="122" android:keyLabel="z" />  
  37.                 <Key android:codes="120" android:keyLabel="x" />  
  38.                 <Key android:codes="99" android:keyLabel="c" />  
  39.                 <Key android:codes="118" android:keyLabel="v" />  
  40.                 <Key android:codes="98" android:keyLabel="b" />  
  41.                 <Key android:codes="110" android:keyLabel="n" />  
  42.                 <Key android:codes="109" android:keyLabel="m" />  
  43.                 <Key android:keyWidth="14.999998%p" android:codes="-5"  
  44.                         android:keyEdgeFlags="right" android:isRepeatable="true"  
  45.                         android:keyIcon="@drawable/sym_keyboard_delete" />  
  46.         </Row>  
  47.         <Row android:rowEdgeFlags="bottom">  
  48.                 <Key android:keyWidth="20.000004%p" android:codes="-2"  
  49.                         android:keyLabel="12#" />  
  50.                 <Key android:keyWidth="14.999998%p" android:codes="44"  
  51.                         android:keyLabel="," />  
  52.                 <Key android:keyWidth="29.999996%p" android:codes="32"  
  53.                         android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />  
  54.                 <Key android:keyWidth="14.999998%p" android:codes="46"  
  55.                         android:keyLabel="." />  
  56.                 <Key android:keyWidth="20.000004%p" android:codes="-3"  
  57.                         android:keyEdgeFlags="right" android:keyLabel="完成" />  
  58.         </Row>  
  59. </Keyboard>  

symbols.xml 内容
[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Keyboard xmlns: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.                 <Key android:codes="49" android:keyLabel="1" />  
  7.                 <Key android:codes="50" android:keyLabel="2" />  
  8.                 <Key android:codes="51" android:keyLabel="3" />  
  9.                 <Key android:codes="57419" android:keyEdgeFlags="right"  
  10.                         android:keyIcon="@drawable/sym_keyboard_left" />  
  11.         </Row>  
  12.         <Row>  
  13.                 <Key android:codes="52" android:keyLabel="4" />  
  14.                 <Key android:codes="53" android:keyLabel="5" />  
  15.                 <Key android:codes="54" android:keyLabel="6" />  
  16.                 <Key android:codes="57421" android:keyEdgeFlags="right"  
  17.                         android:keyIcon="@drawable/sym_keyboard_right" />  
  18.         </Row>  
  19.         <Row>  
  20.                 <Key android:codes="55" android:keyLabel="7" />  
  21.                 <Key android:codes="56" android:keyLabel="8" />  
  22.                 <Key android:codes="57" android:keyLabel="9" />  
  23.                 <Key android:codes="-3" android:keyHeight="100dip"  
  24.                         android:keyEdgeFlags="right" android:isRepeatable="true"  
  25.                         android:keyLabel="完成" />  
  26.         </Row>  
  27.         <Row>  
  28.                 <Key android:codes="-2" android:keyLabel="ABC" />  
  29.                 <Key android:codes="48" android:keyLabel="0" />  
  30.                 <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" />  
  31.         </Row>  
  32. </Keyboard>  

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

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

源码下载地址:http://download.csdn.net/detail/hfsu0419/4534209

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值