keyboard键盘demo
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的更多相关文章
- ros_indigo使用keyboard键盘控制虚拟或实际机器人
首先,上参考网址: http://wiki.ros.org/Robots/TIAGo http://wiki.ros.org/teleop_twist_keyboard 第一个,使用TIAGo的gaz ...
- 表情键盘及文字表情识别简单demo
1.前言 了解了简单图文混排 (属性字符串的使用)及 正则表达式的部分知识,为了加深印象,写了个简单表情键盘demo 展示文字用的是 UITextView 由于时间匆忙,存在一些bug,以及不完善的地 ...
- 安全之路 —— 使用Windows全局钩子打造键盘记录器
简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...
- android开发(45) 自定义软键盘(输入法)
概述 在项目开发中遇到一个需求,”只要数字键盘的输入,仅仅有大写字母的输入,某些输入法总是会提示更新,弹出广告等“,使得我们需要自定义输入. 关联到的知识 KeyboardView 一个视图 ...
- 使用Windows全局钩子打造键盘记录器
简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...
- android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
<activity android:name="xxxActivity" android:configChanges="keyboard|keyboardHidde ...
- h5仿微信、支付宝数字键盘|微信支付键盘|支付宝付款键盘
html5仿微信支付数字键盘|仿支付宝键盘|h5仿微信密码输入键盘|自定义数字键盘 很早之前由于项目需求,就有开发过一个h5仿微信支付键盘,这几天就把之前的数字键盘模块独立出来,重新整理开发成demo ...
- github上老外做的jQuery虚拟键盘
jQuery官方比较迟滞的更新版本: http://plugins.jquery.com/keyboard/ 最新版本更新官方地址: https://github.com/Mottie/Keyboar ...
- iOS Swift WisdomKeyboardKing 键盘智能管家SDK
iOS Swift WisdomKeyboardKing 键盘智能管家SDK [1]前言: 今天给大家推荐个好用的开源框架:WisdomKeyboardKing,方面iOS日常开发,优点和功能请 ...
随机推荐
- [转]SQLServe 存储表结构的几个系统表
1. 获取表的基本字段属性 获取SqlServer中表结构 SELECT syscolumns.name,systypes.name,syscolumns.isnullable, syscolumns ...
- js动态操作订单表格
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- BZOJ 3943: [Usaco2015 Feb]SuperBull 最小生成树
Code: // luogu-judger-enable-o2 #include<bits/stdc++.h> #define setIO(s) freopen(s".in&qu ...
- Selenium3+python自动化 -JS处理滚动条
selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了. 常见场景: 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. 这时候需要借 ...
- HDU 2268 How To Use The Car (数学题)
题目 //做起来很艰辛,总结起来很简单... //注意步行速度可能比车的速度快.... //推公式要仔细,,,, //一道对我来说很搞脑子的数学题,,,,, //车先送第一个人上路,第二个人步行:中途 ...
- 进程映射、mmap(day05)
一.内存管理(续) 每个进程都有自己独立的4G的虚拟地址空间. 冯.诺伊曼体系结构 哈佛体系结构 section 代码段 只读数据段 栈段 举例说明 数据所属的段. 代码参见 memory.c 如何获 ...
- [poj3565] Ants (二分图带权匹配)
传送门 Description 年轻自然主义者比尔在学校研究蚂蚁. 他的蚂蚁以苹果树上苹果为食. 每个蚁群都需要自己的苹果树来养活自己. 比尔有一张坐标为 n 个蚁群和 n 棵苹果树的地图. 他知道蚂 ...
- Springmvc中ajax与jason应用
Springmvc中ajax与jason应用 相关依赖包 json数据转换的jar包 jackson-annotations-2.5.4 jackson-core-2.5.4 jackson-data ...
- Java中indexof()的使用
Java中字符串中子串的查找共有四种方法(indexof()) indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置.如果没有找到子字符串,则返回-1. 如果 starti ...
- ubuntu 配置lamp
官方配置网站:http://wiki.ubuntu.org.cn/LAMP_%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%A ...