先上图


android自定义密码键盘_android


先看一下键盘的布局文件control_digitpasswordkeypad.xml


[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:id="@+id/digitkeypadrootlayout"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical" >
  7.    
  8. <LinearLayout
  9. android:id="@+id/transpwdpdpanel"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_weight="1"
  13. android:background="#000000" >
  14. </LinearLayout>
  15.    
  16. <LinearLayout
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_weight="0"
  20. android:background="@drawable/keyboardbackground"
  21. android:gravity="bottom"
  22. android:orientation="vertical"
  23. android:paddingBottom="4dp"
  24. android:paddingLeft="4dp"
  25. android:paddingRight="4dp" >
  26.    
  27. <LinearLayout
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. android:layout_marginLeft="4dp"
  31. android:layout_marginRight="4dp"
  32. android:orientation="horizontal" >
  33.    
  34. <EditText
  35. android:id="@+id/digitpadedittext"
  36. android:layout_width="fill_parent"
  37. android:layout_height="wrap_content"
  38. android:gravity="center_vertical|right"
  39. android:singleLine="true" />
  40. </LinearLayout>
  41.    
  42. <LinearLayout
  43. android:layout_width="fill_parent"
  44. android:layout_height="wrap_content"
  45. android:orientation="horizontal" >
  46.    
  47. <Button
  48. android:id="@+id/digitkeypad_1"
  49. android:layout_width="wrap_content"
  50. android:layout_height="46dp"
  51. android:layout_marginLeft="4dp"
  52. android:layout_marginRight="2dp"
  53. android:layout_weight="1"
  54. android:background="@drawable/widget_keypad_digit"
  55. android:gravity="center"
  56. android:text="1"
  57. android:textSize="22sp" >
  58. </Button>
  59.    
  60. <Button
  61. android:id="@+id/digitkeypad_2"
  62. android:layout_width="wrap_content"
  63. android:layout_height="46dp"
  64. android:layout_marginLeft="2dp"
  65. android:layout_marginRight="3dp"
  66. android:layout_weight="1"
  67. android:background="@drawable/widget_keypad_digit"
  68. android:gravity="center"
  69. android:text="2"
  70. android:textSize="22sp" >
  71. </Button>
  72.    
  73. <Button
  74. android:id="@+id/digitkeypad_3"
  75. android:layout_width="wrap_content"
  76. android:layout_height="46dp"
  77. android:layout_marginLeft="2dp"
  78. android:layout_marginRight="4dp"
  79. android:layout_weight="1"
  80. android:background="@drawable/widget_keypad_digit"
  81. android:gravity="center"
  82. android:text="3"
  83. android:textSize="22sp" >
  84. </Button>
  85. </LinearLayout>
  86.    
  87. <LinearLayout
  88. android:layout_width="fill_parent"
  89. android:layout_height="wrap_content"
  90. android:orientation="horizontal" >
  91.    
  92. <Button
  93. android:id="@+id/digitkeypad_4"
  94. android:layout_width="wrap_content"
  95. android:layout_height="46dp"
  96. android:layout_marginLeft="4dp"
  97. android:layout_marginRight="2dp"
  98. android:layout_weight="1"
  99. android:background="@drawable/widget_keypad_digit"
  100. android:text="4"
  101. android:textSize="22sp" >
  102. </Button>
  103.    
  104. <Button
  105. android:id="@+id/digitkeypad_5"
  106. android:layout_width="wrap_content"
  107. android:layout_height="46dp"
  108. android:layout_marginLeft="2dp"
  109. android:layout_marginRight="3dp"
  110. android:layout_weight="1"
  111. android:background="@drawable/widget_keypad_digit"
  112. android:text="5"
  113. android:textSize="22sp" >
  114. </Button>
  115.    
  116. <Button
  117. android:id="@+id/digitkeypad_6"
  118. android:layout_width="wrap_content"
  119. android:layout_height="46dp"
  120. android:layout_marginLeft="2dp"
  121. android:layout_marginRight="4dp"
  122. android:layout_weight="1"
  123. android:background="@drawable/widget_keypad_digit"
  124. android:text="6"
  125. android:textSize="22sp" >
  126. </Button>
  127. </LinearLayout>
  128.    
  129. <LinearLayout
  130. android:layout_width="fill_parent"
  131. android:layout_height="wrap_content"
  132. android:orientation="horizontal" >
  133.    
  134. <Button
  135. android:id="@+id/digitkeypad_7"
  136. android:layout_width="80dp"
  137. android:layout_height="46dp"
  138. android:layout_marginLeft="4dp"
  139. android:layout_marginRight="2dp"
  140. android:layout_weight="1"
  141. android:background="@drawable/widget_keypad_digit"
  142. android:text="7"
  143. android:textSize="22sp" >
  144. </Button>
  145.    
  146. <Button
  147. android:id="@+id/digitkeypad_8"
  148. android:layout_width="80dp"
  149. android:layout_height="46dp"
  150. android:layout_marginLeft="2dp"
  151. android:layout_marginRight="3dp"
  152. android:layout_weight="1"
  153. android:background="@drawable/widget_keypad_digit"
  154. android:text="8"
  155. android:textSize="22sp" >
  156. </Button>
  157.    
  158. <Button
  159. android:id="@+id/digitkeypad_9"
  160. android:layout_width="80dp"
  161. android:layout_height="46dp"
  162. android:layout_marginLeft="2dp"
  163. android:layout_marginRight="4dp"
  164. android:layout_weight="1"
  165. android:background="@drawable/widget_keypad_digit"
  166. android:text="9"
  167. android:textSize="22sp" >
  168. </Button>
  169. </LinearLayout>
  170.    
  171. <LinearLayout
  172. android:layout_width="fill_parent"
  173. android:layout_height="wrap_content"
  174. android:orientation="horizontal" >
  175.    
  176. <Button
  177. android:id="@+id/digitkeypad_c"
  178. android:layout_width="80dp"
  179. android:layout_height="46dp"
  180. android:layout_marginLeft="4dp"
  181. android:layout_marginRight="2dp"
  182. android:layout_weight="1"
  183. android:background="@drawable/keyboardclear"
  184. android:text=""
  185. android:textSize="22sp" >
  186. </Button>
  187.    
  188. <Button
  189. android:id="@+id/digitkeypad_0"
  190. android:layout_width="80dp"
  191. android:layout_height="46dp"
  192. android:layout_marginLeft="2dp"
  193. android:layout_marginRight="3dp"
  194. android:layout_weight="1"
  195. android:background="@drawable/widget_keypad_digit"
  196. android:text="0"
  197. android:textSize="22sp" >
  198. </Button>
  199.    
  200. <Button
  201. android:id="@+id/digitkeypad_ok"
  202. android:layout_width="80dp"
  203. android:layout_height="46dp"
  204. android:layout_marginLeft="2dp"
  205. android:layout_marginRight="4dp"
  206. android:layout_weight="1"
  207. android:background="@drawable/keyboardok"
  208. android:text=""
  209. android:textSize="22sp" >
  210. </Button>
  211. </LinearLayout>
  212. </LinearLayout>
  213.    
  214. </LinearLayout>



键盘布局文件截图如下:

android自定义密码键盘_git_02


下面看一下主界面的布局文件mian.xml


[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. <TextView
  8. android:id="@+id/input"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:hint="请输入数字"
  12. />
  13.    
  14. </LinearLayout>



主界面只有一个TextView控件,通过触发TextView的单击事件弹出自定义键盘

触发事件代码:


[java] ​​ view plain​​ ​​copy​​


  1. TextView editText = (TextView)findViewById(R.id.input);  
  2. editText.setOnClickListener(new
  3. @Override
  4. public void
  5.         showPassWdPadView();  
  6.     }  
  7. });  



弹出自定义键盘方法showPassWdPadView()


[java] ​​ view plain​​ ​​copy​​


  1. private void
  2. this.runOnUiThread(new
  3. public void
  4. // 让一个视图浮动在你的应用程序之上
  5.                 WindowManager windowmanager = (WindowManager) content.getSystemService(Context.WINDOW_SERVICE);  
  6. new LayoutParams(-1, -1, WindowManager.LayoutParams.FIRST_SUB_WINDOW, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.RGBA_8888);  
  7.                 layoutparams.gravity = Gravity.BOTTOM;  
  8. 140);  
  9.                 windowmanager.addView(passwdview, layoutparams);  
  10.             }  
  11.         });  
  12. }  



这里用到了WindowManager,具体用法可参考网上。

下面是主界面的全部代码


[java] ​​ view plain​​ ​​copy​​


  1. import
  2. import
  3. import
  4. import
  5. import
  6. import
  7. import
  8. import
  9. import
  10. import
  11.    
  12. public class AndroidInputNumberActivity extends
  13. private
  14. private
  15. private
  16. @Override
  17. public void
  18. super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.           
  21. this;  
  22.           
  23. new DigitPasswordKeyPad(this);  
  24.         passwdview = dpk.setup();  
  25.           
  26.         TextView editText = (TextView)findViewById(R.id.input);  
  27. new
  28. @Override
  29. public void
  30.                 showPassWdPadView();  
  31.             }  
  32.         });  
  33.     }  
  34.       
  35. private void
  36. this.runOnUiThread(new
  37. public void
  38. // 让一个视图浮动在你的应用程序之上
  39.                     WindowManager windowmanager = (WindowManager) content.getSystemService(Context.WINDOW_SERVICE);  
  40. new LayoutParams(-1, -1, WindowManager.LayoutParams.FIRST_SUB_WINDOW, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.RGBA_8888);  
  41.                     layoutparams.gravity = Gravity.BOTTOM;  
  42. 140);  
  43.                     windowmanager.addView(passwdview, layoutparams);  
  44.                 }  
  45.             });  
  46.     }  
  47. }  



上面代码中passwdview这个对象就是弹出键盘界面的View实例,passwdview这个对象是在DigitPasswordKeyPad中初始化得到。

DigitPasswordKeyPad类自定义键盘类,继承自View类


[java] ​​ view plain​​ ​​copy​​


  1. import
  2. import
  3. import
  4. import
  5. import
  6. import
  7.    
  8. /**
  9.  * 系统定制的数字密码键盘
  10.  *
  11.  * @author twf
  12.  *
  13.  */
  14. public class DigitPasswordKeyPad extends
  15. private Context ctx = null;  
  16. private
  17. private String digitnum = "";  
  18. private int length = 6;  
  19.    
  20. private
  21. private
  22. private
  23. private
  24. private
  25. private
  26. private
  27. private
  28. private
  29. private
  30. private
  31. private
  32. private
  33.    
  34. private boolean
  35.    
  36. public
  37. super(ctx);  
  38. this.ctx = ctx;  
  39.     }  
  40.    
  41. @Override
  42. protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int
  43.     }  
  44.    
  45. public void setEditTextIsPwd(boolean
  46. if
  47.             digitkeypad_edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);  
  48. else
  49.             digitkeypad_edittext.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);  
  50.         }  
  51. this.isPwd = ispwd;  
  52.     }  
  53.    
  54. public
  55.         LayoutInflater lif = LayoutInflater.from(ctx);  
  56. null);  
  57.    
  58. // 初始化 对象
  59.         digitkeypad_1 = (Button) v.findViewById(R.id.digitkeypad_1);  
  60.         digitkeypad_2 = (Button) v.findViewById(R.id.digitkeypad_2);  
  61.         digitkeypad_3 = (Button) v.findViewById(R.id.digitkeypad_3);  
  62.         digitkeypad_4 = (Button) v.findViewById(R.id.digitkeypad_4);  
  63.         digitkeypad_5 = (Button) v.findViewById(R.id.digitkeypad_5);  
  64.         digitkeypad_6 = (Button) v.findViewById(R.id.digitkeypad_6);  
  65.         digitkeypad_7 = (Button) v.findViewById(R.id.digitkeypad_7);  
  66.         digitkeypad_8 = (Button) v.findViewById(R.id.digitkeypad_8);  
  67.         digitkeypad_9 = (Button) v.findViewById(R.id.digitkeypad_9);  
  68.         digitkeypad_0 = (Button) v.findViewById(R.id.digitkeypad_0);  
  69.         digitkeypad_c = (Button) v.findViewById(R.id.digitkeypad_c);  
  70.         digitkeypad_ok = (Button) v.findViewById(R.id.digitkeypad_ok);  
  71.         digitkeypad_edittext = (EditText) v.findViewById(R.id.digitpadedittext);  
  72.    
  73. // 添加点击事件
  74. new
  75.         digitkeypad_1.setOnClickListener(dkol);  
  76.         digitkeypad_2.setOnClickListener(dkol);  
  77.         digitkeypad_3.setOnClickListener(dkol);  
  78.         digitkeypad_4.setOnClickListener(dkol);  
  79.         digitkeypad_5.setOnClickListener(dkol);  
  80.         digitkeypad_6.setOnClickListener(dkol);  
  81.         digitkeypad_7.setOnClickListener(dkol);  
  82.         digitkeypad_8.setOnClickListener(dkol);  
  83.         digitkeypad_9.setOnClickListener(dkol);  
  84.         digitkeypad_0.setOnClickListener(dkol);  
  85.         digitkeypad_c.setOnClickListener(dkol);  
  86. new
  87.    
  88. return
  89.     }  
  90.    
  91. private class DigitPasswordKeypadFinshOnClikcListener implements
  92.    
  93. @Override
  94. public void
  95. int
  96.    
  97. if
  98. // 点击完成
  99. // 设置值回页面
  100. // 隐藏自己View
  101. // if (isPwd) {
  102. // jsimpl.hidePasswdPad(digitkeypad_edittext.getText().toString());
  103. // } else {
  104. // jsimpl.hideCallNumPad(digitkeypad_edittext.getText().toString());
  105. // }
  106. this.setVisibility(View.GONE);  
  107.             }  
  108.         }  
  109.     }  
  110.    
  111. public void initInputLable(String str, int
  112.         str = str.trim();  
  113.         digitnum = str;  
  114. this.length = length;  
  115.         digitkeypad_edittext.setText(digitnum);  
  116.         digitkeypad_edittext.setSelection(digitnum.length());  
  117.     }  
  118.    
  119. private class DigitPasswordKeypadOnClickListener implements
  120.    
  121. @Override
  122. public void
  123. int
  124.    
  125. switch
  126. case
  127. if
  128. return;  
  129. else
  130. 1;  
  131.                 }  
  132. break;  
  133. case
  134. if
  135. return;  
  136. else
  137. 2;  
  138.                 }  
  139. break;  
  140. case
  141. if
  142. return;  
  143. else
  144. 3;  
  145.                 }  
  146. break;  
  147. case
  148. if
  149. return;  
  150. else
  151. 4;  
  152.                 }  
  153. break;  
  154. case
  155. if
  156. return;  
  157. else
  158. 5;  
  159.                 }  
  160. break;  
  161. case
  162. if
  163. return;  
  164. else
  165. 6;  
  166.                 }  
  167. break;  
  168. case
  169. if
  170. return;  
  171. else
  172. 7;  
  173.                 }  
  174. break;  
  175. case
  176. if
  177. return;  
  178. else
  179. 8;  
  180.                 }  
  181. break;  
  182. case
  183. if
  184. return;  
  185. else
  186. 9;  
  187.                 }  
  188. break;  
  189. case
  190. if
  191. return;  
  192. else
  193. 0;  
  194.                 }  
  195.    
  196. break;  
  197. case R.id.digitkeypad_c:// 后退
  198. if (digitnum.length() > 0) {  
  199. 0, digitnum.length() - 1);  
  200.                 }  
  201. break;  
  202.             }  
  203. // 格式化 数据
  204.             digitkeypad_edittext.setText(digitnum);  
  205. null != digitnum ? digitnum.length() : 0);  
  206.         }  
  207.    
  208.     }