布局文件

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

主要操作类

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

使用

  1. public void setKeyBoard() {
  2. edit = (EditText)findViewById(R.id.edit);
  3. edit.setInputType(InputType.TYPE_NULL);
  4. edit.setOnTouchListener(new View.OnTouchListener() {
  5. @Override
  6. public boolean onTouch(View view, MotionEvent motionEvent) {
  7. new KeyboardUtil(activity, context, edit).showKeyboard();
  8. return false;
  9. }
  10. });
  11. }

Android自定义键盘的更多相关文章

  1. android自定义键盘(解决弹出提示的字体颜色问题)

    最近准备要做一个项目,需要用到自定义小键盘来确保安全,而且还需要精确获得用户点击键盘时的落点位置.力度.指尖接触屏幕的面积等参数. 在写自定义键盘的时候,用到了国内网上的一些代码,出处是 向先人致敬! ...

  2. Android 自定义键盘车牌号输入

    参考资料 :https://github.com/winterPei/LicensePlateUtil 自定义优化代码: https://gitee.com/anan9303/LicensePlate ...

  3. Android自定义安全键盘

    在银行APP里经常要自定义键盘,例如实现下面这样的效果       首先在xml文件里定义键盘 <Keyboard xmlns:android="http://schemas.andr ...

  4. DownEditTextView【自定义Edittext对Android 软键盘向下的监听】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录自定义EditText控件实现监听软键盘隐藏事件的功能.基本上和参考资料相同. 效果图    代码分析 自定义EditText子 ...

  5. (转)[原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  6. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

  7. [原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  8. Android Studio键盘快捷键

    Android Studio 为许多常用操作提供了键盘快捷键. 表 1 按操作系统显示了默认键盘快捷键. 注:除了下面表 1 中的默认键映射外,您还可以选择多种预设的键映射,也可以创建自定义键映射.如 ...

  9. android自定义view系列:认识activity结构

    标签: android 自定义view activity 开发中虽然我们调用Activity的setContentView(R.layout.activity_main)方法显示View视图,但是vi ...

随机推荐

  1. 优秀的API接口设计原则及方法(转)

    一旦API发生变化,就可能对相关的调用者带来巨大的代价,用户需要排查所有调用的代码,需要调整所有与之相关的部分,这些工作对他们来说都是额外的.如果辛辛苦苦完成这些以后,还发现了相关的bug,那对用户的 ...

  2. 在Salesforce中处理Xml的生成与解析

    在Salesforce中处理Xml的生成与解析 1): Generate Xml private String ConvertAccountToXmlInfo(Account acc){ Dom.Do ...

  3. Convert Object to XML using LINQ

    Convert Object to XML using LINQ. Also the object contains other object list. Following is the Class ...

  4. 在Salesforce中通过dataloadercliq调用data loader来批量处理数据

    上一篇文章讲到,通过data loader去批量处理数据,那么这篇文章将主要讲解在Salesforce中通过dataloadercliq调用data loader来批量处理数据. 1): CLIq文件 ...

  5. 智能车学习(十一)——陀螺仪学习

    一.学习说明 感觉就是配置I2C通信,然后直接移植51代码... 二.代码分享: 1.头文件: #ifndef I2C_GYRO_H_ #define I2C_GYRO_H_ /*********** ...

  6. T-SQL 存储过程

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...

  7. 【MySQL 安装过程2】MySQL安装到 最后一部 未响应 的解决方案

    首先我们应该做的 是在控制面板将MySQL 卸载.再进行以下的操作: 1.在开始菜单下,点击运行,输入regedit,进入注册表编辑器目录下 2.在注册表编辑器里system下找到controlset ...

  8. Android开发中Handler的经典总结

    当应用程序启动时,Android首先会开启一个主线程(也就是UI线程),主线程为管理界面中的UI控件,进行事件分发. AD: 一.Handler的定义: 主要接受子线程发送的数据, 并用此数据配合主线 ...

  9. codeforces 286 div2 B

    思路:质因子累乘的值即为所求#include<iostream> #include<algorithm> #include<stdio.h> #include< ...

  10. 2016.9.14 JavaScript入门之七面向对象和函数

    1.JavaScript中的变量,可以是对象,具有相应的属性: 2.我们也能够使用构造函数创建对象.构造函数的函数给出了一个大写的名称,以使它清楚地表明它是一个构造函数. 在构造函数中,这个变量是指由 ...