package cn.key;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
public class KeyboardUtil {
@SuppressWarnings("unused")
private Context ctx;
@SuppressWarnings("unused")
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;
List<Key> keyList = k2.getKeys();
//查找出0-9的数字键
List<Key> newkeyList = new ArrayList<Key>();
for (int i=0;i<keyList.size();i++) {
if(keyList.get(i).label != null &&
isNumber(keyList.get(i).label.toString())){
newkeyList.add(keyList.get(i));
}
}
//数组长度
int count = newkeyList.size();
//结果集
List<KeyModel> resultList = new ArrayList<KeyModel>();
//用一个LinkedList作为中介
LinkedList<KeyModel> temp = new LinkedList<KeyModel>();
//初始化temp
for(int i = 0; i<count; i++){
temp.add(new KeyModel(48+i, i+""));
}
//取数
Random rand = new Random();
for(int i = 0;i<count; i++){
int num = rand.nextInt(count - i);
resultList.add(new KeyModel(temp.get(num).getCode(),
temp.get(num).getLable()));
temp.remove(num);
}
for(int i=0;i<newkeyList.size();i++){
newkeyList.get(i).label = resultList.get(i).getLable();
newkeyList.get(i).codes[0] = resultList.get(i).getCode();
}
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 isNumber(String str){
String wordstr = "0123456789";
if(wordstr.indexOf(str) > -1){
return true;
}
return false;
}
private boolean isword(String str){
String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase())>-1) {
return true;
}
return false;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android自定义软件盘(数字显示顺序随机)
共65个文件
png:30个
class:15个
xml:8个
5星 · 超过95%的资源 需积分: 10 94 下载量 77 浏览量
2013-07-15
16:12:31
上传
评论 1
收藏 120KB RAR 举报
温馨提示
http://blog.csdn.net/hfsu0419/article/details/7924673参照这个上面改的,本人水平有限或许实现的不是很好,希望高手改进,谢谢!
资源推荐
资源详情
资源评论
收起资源包目录
keydemo.rar (65个子文件)
keydemo
bin
classes.dex 12KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
sym_keyboard_done.png 679B
sym_keyboard_search.png 1KB
ic_launcher.png 4KB
sym_keyboard_delete.png 376B
keyboard_backspace.png 667B
sym_keyboard_space.png 450B
ic_dialog_keyboard.png 491B
sym_keyboard_shift.png 1KB
icon.png 3KB
sym_keyboard_right.PNG 1023B
sym_keyboard_left.PNG 1KB
drawable
normal_key_bg.9.png 761B
normal_key_hl_bg.9.png 685B
drawable-mdpi
ic_launcher.png 2KB
keydemo.apk 40KB
classes
cn
key
R$layout.class 358B
KeydemoActivity$2.class 1KB
KeydemoActivity$1.class 1KB
R$id.class 412B
R$drawable.class 894B
R$dimen.class 361B
R$attr.class 304B
R.class 551B
R$string.class 391B
KeydemoActivity.class 2KB
R$xml.class 382B
KeyboardUtil.class 4KB
R$color.class 2KB
KeyModel.class 854B
KeyboardUtil$1.class 4KB
resources.ap_ 32KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
sym_keyboard_done.png 771B
sym_keyboard_search.png 1KB
ic_launcher.png 4KB
sym_keyboard_delete.png 465B
keyboard_backspace.png 667B
sym_keyboard_space.png 450B
ic_dialog_keyboard.png 528B
sym_keyboard_shift.png 1KB
icon.png 3KB
sym_keyboard_right.PNG 914B
sym_keyboard_left.PNG 919B
drawable
normal_key_bg.9.png 994B
btn_keyboard_key.xml 271B
normal_key_hl_bg.9.png 960B
values
colors.xml 3KB
strings.xml 180B
dimens.xml 111B
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 1KB
xml
symbols.xml 1KB
qwerty.xml 3KB
assets
gen
cn
key
R.java 5KB
proguard.cfg 1KB
.settings
org.eclipse.core.resources.prefs 57B
src
cn
key
KeydemoActivity.java 1KB
KeyboardUtil.java 5KB
KeyModel.java 436B
.project 843B
.classpath 364B
project.properties 360B
AndroidManifest.xml 726B
共 65 条
- 1
资源评论
- 奋斗的奶叔2014-02-27很好,给了拓展的思路
- 充电实践2018-01-26可以参考,学习下
- 算盘记账APP小程序2015-10-02很好,真的很有借鉴价值
- 6724955282013-11-14真的很好,界面很好看
- 临汾阿杜2014-08-28不错的选择,有帮助
苃灬潙
- 粉丝: 1
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- tensorflow-2.7.3-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-2.7.2-cp39-cp39-manylinux2010-x86-64.whl
- Python版本快速排序源代码
- Python 语言版的快速排序算法实现
- 450815388207377安卓_base.apk
- 超微主板 X9DRE-TF+ bios 支持 nvme启动
- 基于Python通过下载气象数据和插值拟合离散数据曲线实现对寒潮过程的能量分析
- 健身房系统的设计与实现论文Java项目
- 使用TCP实现的搜索可用服务器
- 使用贪心算法解决会议时间安排问题的 Java 示例代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功