首页 > 其他分享 >android自定义密码键盘

android自定义密码键盘

时间:2022-12-14 20:39:24浏览次数:44  
标签:layout Overridepublic 自定义 void 键盘 key android true



先看界面布局文件

[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
  2.    
  3. import
  4. import
  5. import
  6. import
  7. import
  8. import
  9. import
  10. import
  11.    
  12. public class KeydemoActivity extends
  13. private
  14. private
  15. private
  16. private
  17.    
  18. @Override
  19. public void
  20. super.onCreate(savedInstanceState);  
  21.                 setContentView(R.layout.main);  
  22. this;  
  23. this;  
  24.    
  25. this.findViewById(R.id.edit);  
  26.                 edit.setInputType(InputType.TYPE_NULL);  
  27.    
  28. this.findViewById(R.id.edit1);  
  29.    
  30. new
  31. @Override
  32. public boolean
  33. new
  34. return false;  
  35.                         }  
  36.                 });  
  37.    
  38. new
  39. @Override
  40. public boolean
  41. int
  42.                                 edit1.setInputType(InputType.TYPE_NULL);  
  43. new
  44.                                 edit1.setInputType(inputback);  
  45. return false;  
  46.                         }  
  47.                 });  
  48.    
  49.         }  
  50. }  



KeyboardUtil.java


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


  1. package
  2.    
  3. import
  4.    
  5. import
  6. import
  7. import
  8. import
  9. import
  10. import
  11. import
  12. import
  13. import
  14.    
  15. public class
  16. private
  17. private
  18. private
  19. private Keyboard k1;// 字母键盘
  20. private Keyboard k2;// 数字键盘
  21. public boolean isnun = false;// 是否数据键盘
  22. public boolean isupper = false;// 是否大写
  23.    
  24. private
  25.    
  26. public
  27. this.act = act;  
  28. this.ctx = ctx;  
  29. this.ed = edit;  
  30. new
  31. new
  32.                 keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);  
  33.                 keyboardView.setKeyboard(k1);  
  34. true);  
  35. true);  
  36.                 keyboardView.setOnKeyboardActionListener(listener);  
  37.         }  
  38.    
  39. private OnKeyboardActionListener listener = new
  40. @Override
  41. public void
  42.                 }  
  43.    
  44. @Override
  45. public void
  46.                 }  
  47.    
  48. @Override
  49. public void
  50.                 }  
  51.    
  52. @Override
  53. public void
  54.                 }  
  55.    
  56. @Override
  57. public void
  58.                 }  
  59.    
  60. @Override
  61. public void onRelease(int
  62.                 }  
  63.    
  64. @Override
  65. public void onPress(int
  66.                 }  
  67.    
  68. @Override
  69. public void onKey(int primaryCode, int[] keyCodes) {  
  70.                         Editable editable = ed.getText();  
  71. int
  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. 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
  86. false;  
  87.                                         keyboardView.setKeyboard(k1);  
  88. else
  89. true;  
  90.                                         keyboardView.setKeyboard(k2);  
  91.                                 }  
  92. else if (primaryCode == 57419) { // go left
  93. if (start > 0) {  
  94. 1);  
  95.                                 }  
  96. else if (primaryCode == 57421) { // go right
  97. if
  98. 1);  
  99.                                 }  
  100. else
  101. char) primaryCode));  
  102.                         }  
  103.                 }  
  104.         };  
  105.            
  106. /**
  107.          * 键盘大小写切换
  108.          */
  109. private void
  110.                 List<Key> keylist = k1.getKeys();  
  111. if (isupper) {//大写切换小写
  112. false;  
  113. for(Key key:keylist){  
  114. if (key.label!=null
  115.                                         key.label = key.label.toString().toLowerCase();  
  116. 0] = key.codes[0]+32;  
  117.                                 }  
  118.                         }  
  119. else {//小写切换大写
  120. true;  
  121. for(Key key:keylist){  
  122. if (key.label!=null
  123.                                         key.label = key.label.toString().toUpperCase();  
  124. 0] = key.codes[0]-32;  
  125.                                 }  
  126.                         }  
  127.                 }  
  128.         }  
  129.    
  130. public void
  131. int
  132. if
  133.             keyboardView.setVisibility(View.VISIBLE);  
  134.         }  
  135.     }  
  136.        
  137. public void
  138. int
  139. if
  140.             keyboardView.setVisibility(View.INVISIBLE);  
  141.         }  
  142.     }  
  143.        
  144. private boolean
  145. "abcdefghijklmnopqrstuvwxyz";  
  146. if (wordstr.indexOf(str.toLowerCase())>-1) {  
  147. return true;  
  148.                 }  
  149. return false;  
  150.     }  
  151.    

标签:layout,Overridepublic,自定义,void,键盘,key,android,true
From: https://blog.51cto.com/u_13657808/5938328

相关文章