android自定义密码键盘

简介: <p><img src="http://www.eoeandroid.com/data/attachment/forum/201208/29/151938b6zwy2h9d9w499yh.png" alt=""><img src="http://www.eoeandroid.com/data/attachment/forum/201208/29/151939cljjxtsuex55s4uj


先看界面布局文件

  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内容

  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 内容
  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
  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
  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.    

目录
相关文章
|
8月前
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
572 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
8月前
|
Android开发
Android自定义view之利用PathEffect实现动态效果
本文介绍如何在Android自定义View中利用`PathEffect`实现动态效果。通过改变偏移量,结合`PathEffect`的子类(如`CornerPathEffect`、`DashPathEffect`、`PathDashPathEffect`等)实现路径绘制的动态变化。文章详细解析了各子类的功能与参数,并通过案例代码展示了如何使用`ComposePathEffect`组合效果,以及通过修改偏移量实现动画。最终效果为一个菱形图案沿路径运动,源码附于文末供参考。
161 0
|
8月前
|
Android开发 开发者
Android自定义view之利用drawArc方法实现动态效果
本文介绍了如何通过Android自定义View实现动态效果,重点使用`drawArc`方法完成圆弧动画。首先通过`onSizeChanged`进行测量,初始化画笔属性,设置圆弧相关参数。核心思路是不断改变圆弧扫过角度`sweepAngle`,并调用`invalidate()`刷新View以实现动态旋转效果。最后附上完整代码与效果图,帮助开发者快速理解并实践这一动画实现方式。
208 0
|
8月前
|
Android开发 数据安全/隐私保护 开发者
Android自定义view之模仿登录界面文本输入框(华为云APP)
本文介绍了一款自定义输入框的实现,包含静态效果、hint值浮动动画及功能扩展。通过组合多个控件完成界面布局,使用TranslateAnimation与AlphaAnimation实现hint文字上下浮动效果,支持密码加密解密显示、去除键盘回车空格输入、光标定位等功能。代码基于Android平台,提供完整源码与attrs配置,方便复用与定制。希望对开发者有所帮助。
155 0
|
8月前
|
XML Java Android开发
Android自定义view之网易云推荐歌单界面
本文详细介绍了如何通过自定义View实现网易云音乐推荐歌单界面的效果。首先,作者自定义了一个圆角图片控件`MellowImageView`,用于绘制圆角矩形图片。接着,通过将布局放入`HorizontalScrollView`中,实现了左右滑动功能,并使用`ViewFlipper`添加图片切换动画效果。文章提供了完整的代码示例,包括XML布局、动画文件和Java代码,最终展示了实现效果。此教程适合想了解自定义View和动画效果的开发者。
397 65
Android自定义view之网易云推荐歌单界面
|
8月前
|
XML 前端开发 Android开发
一篇文章带你走近Android自定义view
这是一篇关于Android自定义View的全面教程,涵盖从基础到进阶的知识点。文章首先讲解了自定义View的必要性及简单实现(如通过三个构造函数解决焦点问题),接着深入探讨Canvas绘图、自定义属性设置、动画实现等内容。还提供了具体案例,如跑马灯、折线图、太极图等。此外,文章详细解析了View绘制流程(measure、layout、draw)和事件分发机制。最后延伸至SurfaceView、GLSurfaceView、SVG动画等高级主题,并附带GitHub案例供实践。适合希望深入理解Android自定义View的开发者学习参考。
754 84
|
8月前
|
前端开发 Android开发 UED
讲讲Android为自定义view提供的SurfaceView
本文详细介绍了Android中自定义View时使用SurfaceView的必要性和实现方式。首先分析了在复杂绘制逻辑和高频界面更新场景下,传统View可能引发卡顿的问题,进而引出SurfaceView作为解决方案。文章通过Android官方Demo展示了SurfaceView的基本用法,包括实现`SurfaceHolder.Callback2`接口、与Activity生命周期绑定、子线程中使用`lockCanvas()`和`unlockCanvasAndPost()`方法完成绘图操作。
249 3
|
8月前
|
Android开发 开发者
Android自定义view之围棋动画(化繁为简)
本文介绍了Android自定义View的动画实现,通过两个案例拓展动态效果。第一个案例基于`drawArc`方法实现单次动画,借助布尔值控制动画流程。第二个案例以围棋动画为例,从简单的小球直线运动到双向变速运动,最终实现循环动画效果。代码结构清晰,逻辑简明,展示了如何化繁为简实现复杂动画,帮助读者拓展动态效果设计思路。文末提供完整源码,适合初学者和进阶开发者学习参考。
161 0
Android自定义view之围棋动画(化繁为简)
|
8月前
|
Java Android开发 开发者
Android自定义view之围棋动画
本文详细介绍了在Android中自定义View实现围棋动画的过程。从测量宽高、绘制棋盘背景,到创建固定棋子及动态棋子,最后通过属性动画实现棋子的移动效果。文章还讲解了如何通过自定义属性调整棋子和棋盘的颜色及动画时长,并优化视觉效果,如添加渐变色让白子更明显。最终效果既可作为围棋动画展示,也可用作加载等待动画。代码完整,适合进阶开发者学习参考。
180 0
|
8月前
|
传感器 Android开发 开发者
Android自定义view之3D正方体
本文介绍了如何通过手势滑动操作实现3D正方体的旋转效果,基于Android自定义View中的GLSurfaceView。相较于使用传感器控制,本文改用事件分发机制(onTouchEvent)处理用户手势输入,调整3D正方体的角度。代码中详细展示了TouchSurfaceView的实现,包括触控逻辑、OpenGL ES绘制3D正方体的核心过程,以及生命周期管理。适合对Android 3D图形开发感兴趣的开发者学习参考。
154 0