Android自定义软键盘的实现


先看界面布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><EditTextandroid:id="@+id/edit"android:layout_width="fill_parent"android:layout_height="wrap_content" /><EditTextandroid:id="@+id/edit1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:password="true" /><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><android.inputmethodservice.KeyboardViewandroid:id="@+id/keyboard_view"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:focusable="true"android:focusableInTouchMode="true"android:background="@color/lightblack"android:keyBackground="@drawable/btn_keyboard_key"android:keyTextColor="@color/white"android:visibility="gone" /></RelativeLayout></LinearLayout>

通过布局文件可以看出界面上有两个输入框,其中一个是密码输入框,界面上还有一个隐藏的键盘控件。
在res下新建xml文件夹,在xml文件夹中新建qwerty.xml和symbols.xml文件. qwerty.xml 是字母键盘布局,symbols.xml 是数字键盘布局,内如如下

qwerty.xml内容

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

symbols.xml 内容
<?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>

KeydemoActivity.java
package cn.key;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;public class KeydemoActivity extends Activity {private Context ctx;private Activity act;private EditText edit;private EditText edit1;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);ctx = this;act = this;edit = (EditText) this.findViewById(R.id.edit);edit.setInputType(InputType.TYPE_NULL);edit1 = (EditText) this.findViewById(R.id.edit1);edit.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {new KeyboardUtil(act, ctx, edit).showKeyboard();return false;}});edit1.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {int inputback = edit1.getInputType();edit1.setInputType(InputType.TYPE_NULL);new KeyboardUtil(act, ctx, edit1).showKeyboard();edit1.setInputType(inputback);return false;}});}
}

KeyboardUtil.java
package cn.key;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;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() {@Overridepublic void swipeUp() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeDown() {}@Overridepublic void onText(CharSequence text) {}@Overridepublic void onRelease(int primaryCode) {}@Overridepublic void onPress(int primaryCode) {}@Overridepublic 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 leftif (start > 0) {ed.setSelection(start - 1);}} else if (primaryCode == 57421) { // go rightif (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;}}

源码下载地址:http://download.csdn.net/detail/hfsu0419/4534209


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/1383033.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

Android自定义键盘(KeyboardView)

目录 1.场景&#xff1a;2.想法&#xff1a;3.开始实现&#xff1a;《一》 在res包下创建xml目录,Keyboard标签来定义键盘布局&#xff1a;《二》创建IKeyboardView类并继承KeyboardView类,设置键盘布局&#xff08;数字和字母&#xff09;《三》 处理自定义键盘按键的点击事件…

关于技术转管理角色的认知

软件质量保障&#xff1a;所寫即所思&#xff5c;一个阿里质量人对测试的所感所悟。 程序员发展的岔路口 技术人做了几年专业工作之后&#xff0c;会来到一个重要的“分岔路口”&#xff0c;一边是专业的技术路线&#xff0c;一边是技术团队的管理路线。不少人就开始犯难&…

什么是响应式设计?列举几种实现响应式设计的方法。

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 什么是响应式设计&#xff1f;⭐ 实现响应式设计的方法⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅&#xff01;这个专栏…

【雕爷学编程】Arduino动手做(12)---霍尔模块之单极性霍尔开关器件AH3144E与线性霍尔传感器AH49E

37款传感器与模块的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&#x…

超级简单方法解决秒杀超卖和重复购买问题

一、基本原则 1、SQL加库存数量判断&#xff1a;防止库存变成负数 2.、数据库加唯一索引&#xff1a;防止用户重复购买 二、举例说明 1、开启事务 2、商品表减库存&#xff0c;库存数量要大于0 update miaosha_goods set stock_count stock_count - 1 where goods.id …

金雅拓超级狗superdog

超级狗是SafeNet在圣天诺基础上为中国本土量身定制的新一代加密狗。 超级狗操作简单&#xff0c;非常安全&#xff0c;功能实用&#xff0c;助您轻松完成高强度加密。 PDF和flv, swf, mpg, dat, avi, wav, mp3等加密&#xff09; 4、文档加密&#xff0c;ppt,word,excel,pdf&am…

赛孚耐SafeNet开发狗超级狗开发入门

本文主要讲解如果使用C#语言来对超级狗进行二次开发&#xff0c;如果仅仅是做个激活的功能&#xff0c;可以参照另一篇博客&#xff0c;地址&#xff1a;http://www.cnblogs.com/dathlin/p/8487842.html 如果疑问&#xff0c;可以加QQ群&#xff1a;592132877 继续主题研究&am…

在Elasticsearch中回测超级趋势线(Supertrend)交叉交易策略

我们已经讨论了好几个单一指标交易策略&#xff0c;其中简单的相对强弱指数&#xff08;RSI&#xff09;交易策略取得的利润最高。 在本文中&#xff0c;我们将使用 Elasticsearch 实现超级趋势线&#xff08;Supertrend&#xff09;交叉交易策略&#xff0c;并比较其性能是否优…

cmake基础(1)——简单项目

本节将围绕着hello world展开介绍如何创建一个简单的项目。 一、最小项目 1.准备工作 首先&#xff0c;新建一个目录01用来存放当前项目&#xff0c;并在目录下创建main.cpp和CMakeLists.txt文件&#xff0c;两者文件内容如下&#xff1a; #include <iostream> using…

Go把Map转成对象

最近使用了Redis的Hash&#xff0c;把一个对象给存储到了hash里面&#xff0c;具体如下&#xff1a; 现在需要从RedisHash缓存里面把结果给取出来&#xff0c;同时赋值到一个对象上面 result, err : global.GVA_REDIS.HGetAll(context.Background(), key).Result() 问题是resul…

优秀的LCD显示效果

优秀的LCD显示效果 效果图&#xff1a; 程序和源代码链接&#xff1a;http://download.csdn.net/download/zhangxiaoyu_sy/10012770

屏幕显示技术

本文主要介绍多种屏幕显示技术&#xff0c;主要是三大类&#xff0c;LCD&#xff0c;LED&#xff0c;OLED。 一、LCD LCD&#xff08;Liquid Crystal Display&#xff09;中文是液晶显示器。 LCD 的显示技术有很多&#xff0c;常见的有TFT&#xff0c;IPS&#xff0c;SLCD等…

物联网开发平台大 PK,谁是最佳 Pick?

通过这篇文章了解现下流行的几款物联网软件平台的各种功能。 作者 | Miyuru Dayarathna 译者 | 弯月&#xff0c;责编 | 郭芮 出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09; 以下为译文&#xff1a; 本文以我们对各大物联网供应商的详细分析为基础&#xff0…

uni-app 2.2 发布,大幅度优化 H5 端性能体验 | 技术头条

作者 | uni-app团队 责编 | 伍杏玲 uni-app 自发布以来&#xff0c;已经服务了几十万开发者。但让我们意外&#xff0c;有大量开发者用uni-app只编写H5版&#xff0c;并没有多端发布。 这其实也符合uni-app的初衷&#xff0c;uni-app的定位并不是需要多端发布时才用uni-app。…

第九代小冰惊喜登场,多端融合且琴棋书画样样精通

谈及智能助手&#xff0c;相信大家都不会漏过小冰这款具有划时代意义的产品。从最初的微软小冰到现在的第九代小冰&#xff0c;AI的技术在不断的演进&#xff0c;而小冰也从最初的贴心助手变成了如今琴棋书画样样精通的人工智能前沿技术载体。 北京时间2021年9月22日&#xff…

渡鸦音箱独家测评: 代表百度AI技术尊严的DuerOS, 用户体验真的能过关吗?

撰文 | 宇多田 在今年三个多小时的世界大会主论坛上&#xff0c;百度把一半时间都留给了渡鸦音箱。 这个造型极其吸睛的四色正方体&#xff0c;暂时代替了无人车&#xff0c;成为李彦宏唯一揣到兜里带到乌镇互联网大会的「百度技术代表作」。 与其他重量级 AI 硬件厂商相比&am…

机器视觉 使用halcon的学习之路

2022.9.23 公司安排说学习机器视觉。 当天下午下载安装了halcon软件。看里面的示例程序。 除了纯英文外&#xff0c;很详细的。点中算子函数&#xff0c;按F1会有详细介绍。按F5执行。按F7单步执行。我随便看看啊&#xff0c;就一直按F5 , 看效果。看不出什么名堂。 2022.9.2…

C#开发工控上位机编程 csdn_机器视觉软件开发新人入门必看 --机器视觉软件开发学习路径...

机器视觉是机械、运动、控制、光学、软件、算法于一体的交叉学科,对于学工科的人来说,机械、运动、控制都有一定的了解,对于软件、算法、光学不是很了解。一台设备,有一个到二个机械设计师或者结构工程师,那么这个角色就胜任了机械部分,有一个电气工程师,那么就胜任了控…

OpenCV与机器视觉

最近在网易云课堂把南科大于仕琪团队的OpenCV教程完整看了一遍&#xff0c;对图像处理或者机器视觉又有了一个系统性的理解。OpenCV中文网站就是他创建的&#xff0c;他的研究团队及其相应成果可以在个人网站中查阅。回想过去在图像处理方面的点点滴滴&#xff0c;做了一个详细…

机器视觉入门 Visual Studio 2015 配置 Opencv3.2

本文主要讲述如何在Visual Studio 2015中配置Opencv3.2版本 例子使用的是WIN 10 系统 Visual Studio 2015 OpenCV 3.2 一&#xff0c;下载OpenCV 3.2 OpenCV 官网 &#xff1a; http://opencv.org/ 请根据自己的操作系统选择相应的 或者可以在GitHub 下载&#xff1a; h…