Android自定义软键盘的设计与实现代码
作者:hfsu0419 发布时间:2023-12-19 11:57:01
标签:android,自定义软键盘
偶然间发现了Android.inputmethodservice.Keyboard类,即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" >
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.inputmethodservice.KeyboardView
android: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;
@Override
public 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() {
@Override
public boolean onTouch(View v, MotionEvent event) {
new KeyboardUtil(act, ctx, edit).showKeyboard();
return false;
}
});
edit1.setOnTouchListener(new OnTouchListener() {
@Override
public 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() {
@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;
}
}
0
投稿
猜你喜欢
- 项目开发中,经常会遇到定时任务的场景,Spring提供了@Scheduled注解,方便进行定时任务的开发概述要使用@Scheduled注解,
- 一段看似平平无奇的代码Classroom classroom = this.getOne( Wrappers.<Class
- 本文实例讲述了Java面向对象程序设计:继承,多态用法。分享给大家供大家参考,具体如下:本文内容:继承多态首发时期:2018-03-23继承
- C# using 三种使用方式介绍1.using指令。using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的
- 本文实例为大家分享了C#用timer实现背单词小程序的具体代码,供大家参考,具体内容如下看到网上有类似的教程视频实现单词本,于是自己敲了一个
- 前言前面我们已经分析Dubbo SPI相关的源码,看过的小伙伴相信已经知晓整个加载过程,我们也留下两个问题,今天我们先来处理下其中关于注解A
- 一、概述1、XMLReader为抽象类,其派生类有:XmlDictionaryReaderXmlNodeReaderXmlTextReade
- 简述在学着使用Java的命令行来编译java文件的时候,遇到了这个问题Windows操作系统报错“‘
- 概述从今天开始, 小白我将带大家开启 Jave 数据结构 & 算法的新篇章.链表链表 (Linked List) 是一种递归的动态数
- 前言相对来说呢,jpg格式的相对来说容易破解一点,当然也取决于你的干扰元素,元素越复杂,破解也就难度越高,有的加的多,人都识别不出来了,何况
- 一、问题Spring2.1.5集成activiti7.1.24时访问要输入用户名和密码。 @Autowired private
- 1.为什么要 token自动续期token中一般会包含用户的基本信息,为了保证token的安全性,一般会将token的过期时间设置的比较短,
- 最近正好也没什么可忙的,就回过头来鼓捣过去的知识点,到Servlet部分时,以前学习的时候硬是把从上到下的继承关系和接口实现记得乱七八糟。这
- C#的多态性:我的理解是:同一个操作,作用于不同的对象时,会有不同的结果,即同一个方法根据需要,作用于不同的对象时,会有不同的实现。C#的多
- 一,下载Zookeeper,地址为http://archive.apache.org/dist/zookeeper/,找到你要下载的版本,我
- 对智能手机有所了解的朋友都知道其中一个应用广泛的手机操作系统Android 开源手机操作系统。那么在这一系统中想要实现通话的监听功能的话,我
- 目录MVC结构简介SpringMVC简介SpringMVC执行流程常用注解1. @Controller2. @ResponseBody3.
- 前言进入到 SpringBoot2.7 时代,有小伙伴发现有一个常用的类忽然过期了:在 Spring Security 时代,这个类可太重要
- 如果你还不是很了解restful,或者认为restful只是一种规范不具有实际意义,推荐一篇osc两年前的文章:RESTful API 设计
- 使用ModelAndView向request域对象共享数据index.html<a th:href="@{/testMode