main.xml

<?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:keyPreviewLayout="@layout/key_preview_layout"
android:visibility="gone" />
</RelativeLayout> </LinearLayout>

key_preview_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="@color/font_red"
android:gravity="center"
android:background="@color/blue"/>

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="" android:keyEdgeFlags="left"
android:keyLabel="q" />
<Key android:codes="" android:keyLabel="w" />
<Key android:codes="" android:keyLabel="e" />
<Key android:codes="" android:keyLabel="r" />
<Key android:codes="" android:keyLabel="t" />
<Key android:codes="" android:keyLabel="y" />
<Key android:codes="" android:keyLabel="u" />
<Key android:codes="" android:keyLabel="i" />
<Key android:codes="" android:keyLabel="o" />
<Key android:codes="" android:keyEdgeFlags="right"
android:keyLabel="p" />
</Row>
<Row>
<Key android:horizontalGap="4.999995%p" android:codes=""
android:keyEdgeFlags="left" android:keyLabel="a" />
<Key android:codes="" android:keyLabel="s" />
<Key android:codes="" android:keyLabel="d" />
<Key android:codes="" android:keyLabel="f" />
<Key android:codes="" android:keyLabel="g" />
<Key android:codes="" android:keyLabel="h" />
<Key android:codes="" android:keyLabel="j" />
<Key android:codes="" android:keyLabel="k" />
<Key android:codes="" 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="" android:keyLabel="z" />
<Key android:codes="" android:keyLabel="x" />
<Key android:codes="" android:keyLabel="c" />
<Key android:codes="" android:keyLabel="v" />
<Key android:codes="" android:keyLabel="b" />
<Key android:codes="" android:keyLabel="n" />
<Key android:codes="" 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=""
android:keyLabel="," />
<Key android:keyWidth="29.999996%p" android:codes=""
android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />
<Key android:keyWidth="14.999998%p" android:codes=""
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="" android:keyLabel="" />
<Key android:codes="" android:keyLabel="" />
<Key android:codes="" android:keyLabel="" />
<Key android:codes="" android:keyEdgeFlags="right"
android:keyIcon="@drawable/sym_keyboard_left" />
</Row>
<Row>
<Key android:codes="" android:keyLabel="" />
<Key android:codes="" android:keyLabel="" />
<Key android:codes="" android:keyLabel="" />
<Key android:codes="" android:keyEdgeFlags="right"
android:keyIcon="@drawable/sym_keyboard_right" />
</Row>
<Row>
<Key android:codes="" android:keyLabel="" />
<Key android:codes="" android:keyLabel="" />
<Key android:codes="" android:keyLabel="" />
<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="" android:keyLabel="" />
<Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" />
</Row>
</Keyboard>

btn_keyboard_key.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/normal_key_hl_bg" />
<item android:drawable="@drawable/normal_key_bg" />
</selector>

KeydemoActivity

package cn.key;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

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

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() > ) {
if (start > ) {
editable.delete(start - , 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 == ) { // go left
if (start > ) {
ed.setSelection(start - );
}
} else if (primaryCode == ) { // go right
if (start < ed.length()) {
ed.setSelection(start + );
}
} 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[] = key.codes[]+;
}
}
} else {//小写切换大写
isupper = true;
for(Key key:keylist){
if (key.label!=null && isword(key.label.toString())) {
key.label = key.label.toString().toUpperCase();
key.codes[] = key.codes[]-;
}
}
}
} 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())>-) {
return true;
}
return false;
} }

keyboard键盘demo的更多相关文章

  1. ros_indigo使用keyboard键盘控制虚拟或实际机器人

    首先,上参考网址: http://wiki.ros.org/Robots/TIAGo http://wiki.ros.org/teleop_twist_keyboard 第一个,使用TIAGo的gaz ...

  2. 表情键盘及文字表情识别简单demo

    1.前言 了解了简单图文混排 (属性字符串的使用)及 正则表达式的部分知识,为了加深印象,写了个简单表情键盘demo 展示文字用的是 UITextView 由于时间匆忙,存在一些bug,以及不完善的地 ...

  3. 安全之路 —— 使用Windows全局钩子打造键盘记录器

    简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...

  4. android开发(45) 自定义软键盘(输入法)

    概述 在项目开发中遇到一个需求,”只要数字键盘的输入,仅仅有大写字母的输入,某些输入法总是会提示更新,弹出广告等“,使得我们需要自定义输入. 关联到的知识 KeyboardView      一个视图 ...

  5. 使用Windows全局钩子打造键盘记录器

    简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...

  6. android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

    <activity android:name="xxxActivity" android:configChanges="keyboard|keyboardHidde ...

  7. h5仿微信、支付宝数字键盘|微信支付键盘|支付宝付款键盘

    html5仿微信支付数字键盘|仿支付宝键盘|h5仿微信密码输入键盘|自定义数字键盘 很早之前由于项目需求,就有开发过一个h5仿微信支付键盘,这几天就把之前的数字键盘模块独立出来,重新整理开发成demo ...

  8. github上老外做的jQuery虚拟键盘

    jQuery官方比较迟滞的更新版本: http://plugins.jquery.com/keyboard/ 最新版本更新官方地址: https://github.com/Mottie/Keyboar ...

  9. iOS Swift WisdomKeyboardKing 键盘智能管家SDK

    iOS Swift WisdomKeyboardKing 键盘智能管家SDK [1]前言:    今天给大家推荐个好用的开源框架:WisdomKeyboardKing,方面iOS日常开发,优点和功能请 ...

随机推荐

  1. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  2. 利用windbg获取dump的dll文件

    根据堆栈对应的地址查找其对应的Module ID,然后将对应的Module保存. !IP2MD 命令从托管函数中获取 MethodDesc 结构地址. !dumpmodule 1caa50 下面的命令 ...

  3. 3.sql基础

    sql语句是和dbms交谈专用的语句,不同dbms都认sql语法 sql语句中字符串用单引号 sql语句是大小写不敏感的,不敏感指的是sql关键字,字符串值还是大小写敏感的 创建表.删除表不仅可以手工 ...

  4. 【转】js中几种实用的跨域方法原理详解

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...

  5. Android6.0以上版本获取本机蓝牙地址

    Android6.0以上版本使用BluetoothAdapter.getDefaultAdapter().getAddress()是获取不到正确的蓝牙地址的,返回的值都是02:00:00:00:00: ...

  6. Android App 开机启动画面和开机自动启动APP程序设置

    1.当前比较成熟一点的应用基本上都会在进入应用之显示一个启动界面 如腾讯微博 2.准备元素  需要开机启动的图片一张 3.新建Activity AlphaAnimation动画:控制对象alpha水平 ...

  7. 【Oracle】表连接三种方式

    表连接的方式有三种分别是:排序合并连接(Sort Merge Join).嵌套循环连接(Nested Loops Join).哈希连接(Hash Join). 1. 排序合并连接(Sort Merge ...

  8. linux环境jdk安装及配置

    linux环境jdk安装及配置 linux环境jdk安装及配置 1.下载jkd( http://www.oracle.com/technetwork/java/javase/downloads/ind ...

  9. (转) OpenLayers3基础教程——加载资源

    概述: 本节讲述如何在Ol3中加载wms图层并显示到地图中. Ol3下载: 你可以在OL官网去下载,下载地址为http://openlayers.org/download/,也可以去我的百度云盘下载, ...

  10. Jquery常见操作多选框/复选框/checkbox

    1.判断checkbox是否为选中状态: if($("#searchNews").attr("checked")=="checked") { ...