您当前的位置: 首页 >  android

xiangzhihong8

暂无认证

  • 1浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

android自定义密码键盘

xiangzhihong8 发布时间:2014-01-25 16:05:33 ,浏览量:1

先看界面布局文件

[html] view plain copy
  1.   
  2.   
  3.    
  4.       
  5.    
  6.       
  7.    
  8.       
  9.    
  10.           
  11.       
  12.    
  13.   
通过布局文件可以看出界面上有两个输入框,其中一个是密码输入框,界面上还有一个隐藏的键盘控件。 在res下新建xml文件夹,在xml文件夹中新建qwerty.xml和symbols.xml文件. qwerty.xml 是字母键盘布局,symbols.xml 是数字键盘布局,内如如下

qwerty.xml内容

[html] view plain copy
  1.   
  2.   
  3.           
  4.                   
  5.                   
  6.                   
  7.                   
  8.                   
  9.                   
  10.                   
  11.                   
  12.                   
  13.                   
  14.           
  15.           
  16.                   
  17.                   
  18.                   
  19.                   
  20.                   
  21.                   
  22.                   
  23.                   
  24.                   
  25.           
  26.           
  27.                   
  28.                   
  29.                   
  30.                   
  31.                   
  32.                   
  33.                   
  34.                   
  35.                   
  36.           
  37.           
  38.                   
  39.                   
  40.                   
  41.                   
  42.                   
  43.           
  44.   
symbols.xml 内容
[html] view plain copy
  1.   
  2.   
  3.           
  4.                   
  5.                   
  6.                   
  7.                   
  8.           
  9.           
  10.                   
  11.                   
  12.                   
  13.                   
  14.           
  15.           
  16.                   
  17.                   
  18.                   
  19.                   
  20.           
  21.           
  22.                   
  23.                   
  24.                   
  25.           
  26.   
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 -1) {  
  98.                         return true;  
  99.                 }  
  100.             return false;  
  101.     }  
  102.    
关注
打赏
1482932726
查看更多评论
立即登录/注册

微信扫码登录

0.0554s