Android自定义键盘
布局文件
- <?xml version="1.0" encoding="UTF-8"?>
- <Keyboard android:keyWidth="10.000002%p" android:keyHeight="@dimen/key_height"
- android:horizontalGap="0.0px" android:verticalGap="0.0px"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <Row>
- <Key android:codes="113" android:keyEdgeFlags="left"
- android:keyLabel="q" />
- <Key android:codes="119" android:keyLabel="w" />
- <Key android:codes="101" android:keyLabel="e" />
- <Key android:codes="114" android:keyLabel="r" />
- <Key android:codes="116" android:keyLabel="t" />
- <Key android:codes="121" android:keyLabel="y" />
- <Key android:codes="117" android:keyLabel="u" />
- <Key android:codes="105" android:keyLabel="i" />
- <Key android:codes="111" android:keyLabel="o" />
- <Key android:codes="112" android:keyEdgeFlags="right"
- android:keyLabel="p" />
- </Row>
- <Row>
- <Key android:horizontalGap="4.999995%p" android:codes="97"
- android:keyEdgeFlags="left" android:keyLabel="a" />
- <Key android:codes="115" android:keyLabel="s" />
- <Key android:codes="100" android:keyLabel="d" />
- <Key android:codes="102" android:keyLabel="f" />
- <Key android:codes="103" android:keyLabel="g" />
- <Key android:codes="104" android:keyLabel="h" />
- <Key android:codes="106" android:keyLabel="j" />
- <Key android:codes="107" android:keyLabel="k" />
- <Key android:codes="108" android:keyEdgeFlags="right"
- android:keyLabel="l" />
- </Row>
- <Row>
- <Key android:keyWidth="14.999998%p" android:codes="-1"
- android:keyEdgeFlags="left" android:isModifier="true"
- android:isSticky="true" android:keyIcon="@drawable/sym_keyboard_shift" />
- <Key android:codes="122" android:keyLabel="z" />
- <Key android:codes="120" android:keyLabel="x" />
- <Key android:codes="99" android:keyLabel="c" />
- <Key android:codes="118" android:keyLabel="v" />
- <Key android:codes="98" android:keyLabel="b" />
- <Key android:codes="110" android:keyLabel="n" />
- <Key android:codes="109" android:keyLabel="m" />
- <Key android:keyWidth="14.999998%p" android:codes="-5"
- android:keyEdgeFlags="right" android:isRepeatable="true"
- android:keyIcon="@drawable/sym_keyboard_delete" />
- </Row>
- <Row android:rowEdgeFlags="bottom">
- <Key android:keyWidth="20.000004%p" android:codes="-2"
- android:keyLabel="12#" />
- <Key android:keyWidth="14.999998%p" android:codes="44"
- android:keyLabel="," />
- <Key android:keyWidth="29.999996%p" android:codes="32"
- android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />
- <Key android:keyWidth="14.999998%p" android:codes="46"
- android:keyLabel="." />
- <Key android:keyWidth="20.000004%p" android:codes="-3"
- android:keyEdgeFlags="right" android:keyLabel="完成" />
- </Row>
- </Keyboard>
- <?xml version="1.0" encoding="utf-8"?>
- <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
- android:keyWidth="25%p" android:horizontalGap="0px"
- android:verticalGap="0px" android:keyHeight="@dimen/key_height">
- <Row>
- <Key android:codes="49" android:keyLabel="1" />
- <Key android:codes="50" android:keyLabel="2" />
- <Key android:codes="51" android:keyLabel="3" />
- <Key android:codes="57419" android:keyEdgeFlags="right"
- android:keyIcon="@drawable/sym_keyboard_left" />
- </Row>
- <Row>
- <Key android:codes="52" android:keyLabel="4" />
- <Key android:codes="53" android:keyLabel="5" />
- <Key android:codes="54" android:keyLabel="6" />
- <Key android:codes="57421" android:keyEdgeFlags="right"
- android:keyIcon="@drawable/sym_keyboard_right" />
- </Row>
- <Row>
- <Key android:codes="55" android:keyLabel="7" />
- <Key android:codes="56" android:keyLabel="8" />
- <Key android:codes="57" android:keyLabel="9" />
- <Key android:codes="-3" android:keyHeight="100dip"
- android:keyEdgeFlags="right" android:isRepeatable="true"
- android:keyLabel="完成" />
- </Row>
- <Row>
- <Key android:codes="-2" android:keyLabel="ABC" />
- <Key android:codes="48" android:keyLabel="0" />
- <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" />
- </Row>
- </Keyboard>
主要操作类
- package com.example.administrator.yunphone.UI;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.inputmethodservice.Keyboard;
- import android.inputmethodservice.KeyboardView;
- import android.inputmethodservice.Keyboard.Key;
- import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
- import android.text.Editable;
- import android.view.View;
- import android.widget.EditText;
- import com.example.administrator.yunphone.R;
- public class KeyboardUtil {
- private Context ctx;
- private Activity act;
- private KeyboardView keyboardView;
- private Keyboard k1;// 字母键盘
- private Keyboard k2;// 数字键盘
- public boolean isnun = false;// 是否数据键盘
- public boolean isupper = false;// 是否大写
- private EditText ed;
- public KeyboardUtil(Activity act, Context ctx, EditText edit) {
- this.act = act;
- this.ctx = ctx;
- this.ed = edit;
- k1 = new Keyboard(ctx, R.xml.qwerty);
- k2 = new Keyboard(ctx, R.xml.symbols);
- keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
- keyboardView.setKeyboard(k1);
- keyboardView.setEnabled(true);
- keyboardView.setPreviewEnabled(true);
- keyboardView.setOnKeyboardActionListener(listener);
- }
- private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
- @Override
- public void swipeUp() {
- }
- @Override
- public void swipeRight() {
- }
- @Override
- public void swipeLeft() {
- }
- @Override
- public void swipeDown() {
- }
- @Override
- public void onText(CharSequence text) {
- }
- @Override
- public void onRelease(int primaryCode) {
- }
- @Override
- public void onPress(int primaryCode) {
- }
- @Override
- public void onKey(int primaryCode, int[] keyCodes) {
- Editable editable = ed.getText();
- int start = ed.getSelectionStart();
- if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
- hideKeyboard();
- } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
- if (editable != null && editable.length() > 0) {
- if (start > 0) {
- editable.delete(start - 1, start);
- }
- }
- } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
- changeKey();
- keyboardView.setKeyboard(k1);
- } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
- if (isnun) {
- isnun = false;
- keyboardView.setKeyboard(k1);
- } else {
- isnun = true;
- keyboardView.setKeyboard(k2);
- }
- } else if (primaryCode == 57419) { // go left
- if (start > 0) {
- ed.setSelection(start - 1);
- }
- } else if (primaryCode == 57421) { // go right
- if (start < ed.length()) {
- ed.setSelection(start + 1);
- }
- } else {
- editable.insert(start, Character.toString((char) primaryCode));
- }
- }
- };
- /**
- * 键盘大小写切换
- */
- private void changeKey() {
- List<Key> keylist = k1.getKeys();
- if (isupper) {//大写切换小写
- isupper = false;
- for(Key key:keylist){
- if (key.label!=null && isword(key.label.toString())) {
- key.label = key.label.toString().toLowerCase();
- key.codes[0] = key.codes[0]+32;
- }
- }
- } else {//小写切换大写
- isupper = true;
- for(Key key:keylist){
- if (key.label!=null && isword(key.label.toString())) {
- key.label = key.label.toString().toUpperCase();
- key.codes[0] = key.codes[0]-32;
- }
- }
- }
- }
- public void showKeyboard() {
- int visibility = keyboardView.getVisibility();
- if (visibility == View.GONE || visibility == View.INVISIBLE) {
- keyboardView.setVisibility(View.VISIBLE);
- }
- }
- public void hideKeyboard() {
- int visibility = keyboardView.getVisibility();
- if (visibility == View.VISIBLE) {
- keyboardView.setVisibility(View.INVISIBLE);
- }
- }
- private boolean isword(String str){
- String wordstr = "abcdefghijklmnopqrstuvwxyz";
- if (wordstr.indexOf(str.toLowerCase())>-1) {
- return true;
- }
- return false;
- }
- }
使用
- public void setKeyBoard() {
- edit = (EditText)findViewById(R.id.edit);
- edit.setInputType(InputType.TYPE_NULL);
- edit.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View view, MotionEvent motionEvent) {
- new KeyboardUtil(activity, context, edit).showKeyboard();
- return false;
- }
- });
- }
Android自定义键盘的更多相关文章
- android自定义键盘(解决弹出提示的字体颜色问题)
最近准备要做一个项目,需要用到自定义小键盘来确保安全,而且还需要精确获得用户点击键盘时的落点位置.力度.指尖接触屏幕的面积等参数. 在写自定义键盘的时候,用到了国内网上的一些代码,出处是 向先人致敬! ...
- Android 自定义键盘车牌号输入
参考资料 :https://github.com/winterPei/LicensePlateUtil 自定义优化代码: https://gitee.com/anan9303/LicensePlate ...
- Android自定义安全键盘
在银行APP里经常要自定义键盘,例如实现下面这样的效果 首先在xml文件里定义键盘 <Keyboard xmlns:android="http://schemas.andr ...
- DownEditTextView【自定义Edittext对Android 软键盘向下的监听】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录自定义EditText控件实现监听软键盘隐藏事件的功能.基本上和参考资料相同. 效果图 代码分析 自定义EditText子 ...
- (转)[原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- Android 自定义View合集
自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...
- [原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- Android Studio键盘快捷键
Android Studio 为许多常用操作提供了键盘快捷键. 表 1 按操作系统显示了默认键盘快捷键. 注:除了下面表 1 中的默认键映射外,您还可以选择多种预设的键映射,也可以创建自定义键映射.如 ...
- android自定义view系列:认识activity结构
标签: android 自定义view activity 开发中虽然我们调用Activity的setContentView(R.layout.activity_main)方法显示View视图,但是vi ...
随机推荐
- 函数fgets和fputs、fread和fwrite、fscanf和fprintf用法小结 (转)
函数fgets和fputs.fread和fwrite.fscanf和fprintf用法小结 字符串读写函数fgets和fputs 一.读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符 ...
- python最简单的http服务器
人生苦短,我用python 今天有个需求就是简单的把自己的图片通过web共享,自然就想起了使用服务器了,在python下使用一个简单的服务器是非常方便的,用到标准库里面的SimpleHTTPServe ...
- cocos2dx游戏开发——别踩白块学习笔记(二)——经典模式的实现
一.创建GameScene以及GameLayer 就是简单创建一个Scene而已,在此就不多说啦~,可以参照我的打飞机的学习笔记(2). 二.添加一个开始栏 很简单,就是调用Block中的create ...
- zookeeper源码分析二FASTLEADER选举算法
如何在zookeeper集群中选举出一个leader,zookeeper使用了三种算法,具体使用哪种算法,在配置文件中是可以配置的,对应的配置项是"electionAlg",其中1 ...
- 【SQL 触发器】
一.MySQL上触发器的使用 示例: CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW t ...
- vim 标准环境的配置
最近刚刚从IDE转到了vim,很多习惯不一致,特地配置了一下vim环境.在网上找了大神的帖子,怕忘记了,特此纪念. 传送门 http://www.cnblogs.com/ma6174/arch ...
- MySQL主主复制3
一.创建并授权用户 在每一台(主)服务器上创建一个用户,并为之授权,使它们可以互相访问彼此的数据库 在Server-1上: 创建一个充许Server-2来访问的用户server2,密码为:server ...
- css的重置和原子类的使用
@charset "utf-8";/* Reset Definitions*/body, div, p, a, ul, ol, li, dl, dt, dd, h1, h2, h3 ...
- John the Ripper
John the RipperJohn the Ripper(简称John)是一款著名的密码破解工具.它主要针对各种Hash加密的密文.它不同于Rainbow Table方式.它采用实时运算的方式和密 ...
- ajax轮循
使用 AJAX 进行异步加载轮询操作.简单代码如下: <script> // 执行ajax轮循操作 function polling(){ var xmlhttp; // 判断浏览器--创 ...