- 浏览: 185390 次
- 性别:
- 来自: 浙江
文章分类
最新评论
参考连接:http://blog.csdn.net/hfsu0419/article/details/7924673
由于项目需求,需要对软键盘中字符键自定义,实例项目如下。
首先在layout下创建布局文件activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="none" />
<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="@android:color/darker_gray"
android:keyTextColor="#00ff00"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
接着是创建键盘的各个键的分布文件custom.xml,并把它放在xml文件下:
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="0.0px"
android:keyHeight="50dp"
android:keyWidth="10.000002%p"
android:verticalGap="0.0px" >
<Row>
<Key
android:codes="49"
android:keyEdgeFlags="left"
android:keyLabel="1" />
<Key
android:codes="50"
android:keyLabel="2" />
<Key
android:codes="51"
android:keyLabel="3" />
<Key
android:codes="52"
android:keyLabel="4" />
<Key
android:codes="53"
android:keyLabel="5" />
<Key
android:codes="54"
android:keyLabel="6" />
<Key
android:codes="55"
android:keyLabel="7" />
<Key
android:codes="56"
android:keyLabel="8" />
<Key
android:codes="57"
android:keyLabel="9" />
<Key
android:codes="48"
android:keyEdgeFlags="right"
android:keyLabel="0" />
</Row>
<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:codes="97"
android:horizontalGap="4.999995%p"
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:codes="-1"
android:isModifier="true"
android:isSticky="true"
android:keyEdgeFlags="left"
android:keyLabel="大写"
android:keyWidth="14.999998%p" />
<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:codes="-5"
android:isRepeatable="true"
android:keyEdgeFlags="right"
android:keyLabel="退格"
android:keyWidth="14.999998%p" />
</Row>
<Row android:rowEdgeFlags="bottom" >
<Key
android:codes="44"
android:keyEdgeFlags="left"
android:keyLabel=","
android:keyWidth="25%p" />
<Key
android:codes="32"
android:isRepeatable="true"
android:keyLabel="空格"
android:keyWidth="25%p" />
<Key
android:codes="46"
android:keyLabel="."
android:keyWidth="25%p" />
<Key
android:codes="-3"
android:keyEdgeFlags="right"
android:keyLabel="完成"
android:keyWidth="25%p" />
</Row>
</Keyboard>
创建键盘工具类KeyboardUtil.java:
public class KeyboardUtil {
private KeyboardView keyboardView;
private Keyboard k1;
public boolean isupper = false;// 是否大写
boolean isShow=false;
private EditText ed;
public KeyboardUtil(Activity act, Context ctx, EditText edit) {
this.ed = edit;
k1 = new Keyboard(ctx, R.xml.custom);
keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
keyboardView.setKeyboard(k1);
keyboardView.setEnabled(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 == 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 if(key.label.toString().equals("小写")){
key.label="大写";
}
}
} 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;
} else if(key.label.toString().equals("大写")){
key.label="小写";
}
}
}
}
public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
keyboardView.setVisibility(View.VISIBLE);
isShow=true;
}
}
public void hideKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE) {
keyboardView.setVisibility(View.INVISIBLE);
isShow=false;
}
}
private boolean isword(String str){
String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase())>-1) {
return true;
}
return false;
}
}
最后在activity中调用KeyboardUtil:
public class KeydemoActivity extends Activity {
private Context ctx;
private Activity act;
private EditText edit;
KeyboardUtil util;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = this;
act = this;
edit = (EditText) this.findViewById(R.id.edit1);
if (android.os.Build.VERSION.SDK_INT <= 10) {
edit.setInputType(InputType.TYPE_NULL);
} else {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
try {
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus;
// setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setShowSoftInputOnFocus = cls.getMethod("setSoftInputShownOnFocus", boolean.class);
//4.0的是setShowSoftInputOnFocus,4.2的是setSoftInputOnFocus
setShowSoftInputOnFocus.setAccessible(false);
setShowSoftInputOnFocus.invoke(edit, false);
} catch (Exception e) {
e.printStackTrace();
}
}
util=new KeyboardUtil(act, ctx, edit);
edit.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
util.showKeyboard();
return false;
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
if(util.isShow){
util.hideKeyboard();
}else{
finish();
}
return false;
}
return super.onKeyDown(keyCode, event);
}
}
注意红色字段,这是在处理光标出现的问题,刚开始用注销的的方法,测试不成功,调试后发现是方法名错误,改正好成功。
由于项目需求,需要对软键盘中字符键自定义,实例项目如下。
首先在layout下创建布局文件activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="none" />
<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="@android:color/darker_gray"
android:keyTextColor="#00ff00"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
接着是创建键盘的各个键的分布文件custom.xml,并把它放在xml文件下:
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="0.0px"
android:keyHeight="50dp"
android:keyWidth="10.000002%p"
android:verticalGap="0.0px" >
<Row>
<Key
android:codes="49"
android:keyEdgeFlags="left"
android:keyLabel="1" />
<Key
android:codes="50"
android:keyLabel="2" />
<Key
android:codes="51"
android:keyLabel="3" />
<Key
android:codes="52"
android:keyLabel="4" />
<Key
android:codes="53"
android:keyLabel="5" />
<Key
android:codes="54"
android:keyLabel="6" />
<Key
android:codes="55"
android:keyLabel="7" />
<Key
android:codes="56"
android:keyLabel="8" />
<Key
android:codes="57"
android:keyLabel="9" />
<Key
android:codes="48"
android:keyEdgeFlags="right"
android:keyLabel="0" />
</Row>
<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:codes="97"
android:horizontalGap="4.999995%p"
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:codes="-1"
android:isModifier="true"
android:isSticky="true"
android:keyEdgeFlags="left"
android:keyLabel="大写"
android:keyWidth="14.999998%p" />
<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:codes="-5"
android:isRepeatable="true"
android:keyEdgeFlags="right"
android:keyLabel="退格"
android:keyWidth="14.999998%p" />
</Row>
<Row android:rowEdgeFlags="bottom" >
<Key
android:codes="44"
android:keyEdgeFlags="left"
android:keyLabel=","
android:keyWidth="25%p" />
<Key
android:codes="32"
android:isRepeatable="true"
android:keyLabel="空格"
android:keyWidth="25%p" />
<Key
android:codes="46"
android:keyLabel="."
android:keyWidth="25%p" />
<Key
android:codes="-3"
android:keyEdgeFlags="right"
android:keyLabel="完成"
android:keyWidth="25%p" />
</Row>
</Keyboard>
创建键盘工具类KeyboardUtil.java:
public class KeyboardUtil {
private KeyboardView keyboardView;
private Keyboard k1;
public boolean isupper = false;// 是否大写
boolean isShow=false;
private EditText ed;
public KeyboardUtil(Activity act, Context ctx, EditText edit) {
this.ed = edit;
k1 = new Keyboard(ctx, R.xml.custom);
keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
keyboardView.setKeyboard(k1);
keyboardView.setEnabled(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 == 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 if(key.label.toString().equals("小写")){
key.label="大写";
}
}
} 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;
} else if(key.label.toString().equals("大写")){
key.label="小写";
}
}
}
}
public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
keyboardView.setVisibility(View.VISIBLE);
isShow=true;
}
}
public void hideKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE) {
keyboardView.setVisibility(View.INVISIBLE);
isShow=false;
}
}
private boolean isword(String str){
String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase())>-1) {
return true;
}
return false;
}
}
最后在activity中调用KeyboardUtil:
public class KeydemoActivity extends Activity {
private Context ctx;
private Activity act;
private EditText edit;
KeyboardUtil util;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = this;
act = this;
edit = (EditText) this.findViewById(R.id.edit1);
if (android.os.Build.VERSION.SDK_INT <= 10) {
edit.setInputType(InputType.TYPE_NULL);
} else {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
try {
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus;
// setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setShowSoftInputOnFocus = cls.getMethod("setSoftInputShownOnFocus", boolean.class);
//4.0的是setShowSoftInputOnFocus,4.2的是setSoftInputOnFocus
setShowSoftInputOnFocus.setAccessible(false);
setShowSoftInputOnFocus.invoke(edit, false);
} catch (Exception e) {
e.printStackTrace();
}
}
util=new KeyboardUtil(act, ctx, edit);
edit.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
util.showKeyboard();
return false;
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
if(util.isShow){
util.hideKeyboard();
}else{
finish();
}
return false;
}
return super.onKeyDown(keyCode, event);
}
}
注意红色字段,这是在处理光标出现的问题,刚开始用注销的的方法,测试不成功,调试后发现是方法名错误,改正好成功。
- KeyBoardText.rar (1.3 MB)
- 下载次数: 312
发表评论
-
关于Android的webSocket的简单使用
2017-05-12 14:34 987使用第三方jar: autobahn-0.5.0.jar 连 ... -
Comparator自定义排序的使用
2017-05-11 14:18 749java对于集合的自定义排序方法有: Arrays.sort ... -
查看Android虚拟机文件相关命令
2017-05-02 14:12 608我们在虚拟机下添加文件后,想查看下文件是否正确创建,可在win ... -
android6.0创建文件问题
2017-05-02 12:58 587Android在6.0有了动态权限管理,在文件创建时就需要动态 ... -
xutils的http模块的简单使用
2017-04-28 16:05 527先导入xutils相关依赖: compile 'org.xut ... -
Litepal的简单使用
2017-04-27 17:21 642相关包下载地址:https://github.com/Lite ... -
Android下拉刷新上拉加载控件的使用
2017-04-21 10:46 852参考链接:http://www.jianshu.com/p/6 ... -
图片加载框架
2017-04-19 16:29 402图片加载框架: picasso ImageLoader -
Android的Service总结
2017-04-17 15:46 461参考链接:http://www.cnblogs.com/lwb ... -
解决ViewPager的addOnPageChangeListener不加载第一个的问题
2017-03-18 17:53 2649今天在使用ViewPager的时候发现个问题。 需求如下: ... -
clone方法的使用
2017-01-04 10:14 555由于Java有引用这一说,当两个变量指向同一块内存时,改变 ... -
Fragment无法切换问题
2016-12-16 14:57 2114Android有一个回收机制,当内存不足时,会自动回收相关内存 ... -
关于Material Design的CollapsingToolbarLayout初次使用
2016-12-16 13:38 614最近了解了下CollapsingToolbarLayout的使 ... -
MVP模式的学习
2016-12-10 15:15 676以前我写代码都是使用MVC模式,这种模式使Activity变得 ... -
Material Design:Android Design Support Library 介绍
2016-12-10 14:14 471参考链接 : https://blog.leancloud.c ... -
SpannableString简介
2016-12-10 14:03 424参考链接: http://www.cnblogs.com/ji ... -
getResources().getDrawable方法的废弃
2016-12-10 13:20 1236参考链接:http://www.jianshu.com/p/e ... -
关于AndroidStudio的Unsupported major.minor version 52.0异常
2016-12-10 13:15 2380参考链接:http://blog.csdn.net/fakin ... -
Android记录
2015-06-01 10:54 642http://tools.android-studio.org ... -
android自定义控件相关使用
2015-04-24 16:53 612用代码简单介绍下自定义控件的使用: 先看主activity: ...
相关推荐
总之,创建Android自定义软键盘涉及到了服务组件、UI布局设计、事件处理等多个方面。理解这一过程不仅有助于提升Android开发技能,还能为特定应用场景提供更优质的用户体验。通过不断学习和实践,开发者可以打造出...
首先,创建自定义软键盘的核心在于定义一个`InputMethodService`。这个服务类是Android系统用于处理软键盘逻辑的基类。你需要在`AndroidManifest.xml`中声明这个服务,并在服务中重写关键方法,如`onCreateInputView...
总之,创建自定义软键盘需要对Android的InputMethodService有深入理解,并熟练掌握布局设计、事件处理和输入上下文管理。通过`keydemo`项目,你可以进一步学习具体的实现细节,包括XML布局的构建、服务的注册以及...
本文将详细讲解如何实现一个Android自定义软键盘,并介绍如何在TV设备上通过鼠标和遥控器进行操作。我们以"调用android自定义软键盘demo"为例,来探讨这一主题。 首先,创建自定义软键盘的关键在于创建一个新的`...
在Android开发中,有时我们需要为应用提供特定的输入方式,这时就需要自定义软键盘。`KeyboardView`是Android SDK提供的一种组件,它允许开发者创建和显示一个可交互的虚拟键盘。本文将深入探讨如何利用`...
总之,自定义软键盘是Android开发中的一个重要技巧,它允许开发者创建符合应用需求的特殊输入方式。通过理解`Keyboard`、`KeyboardView`以及事件处理机制,开发者可以轻松地为用户提供更加个性化和高效的操作体验。
在Android开发中,自定义软键盘和理解系统键盘的工作原理是提升用户体验的重要一环。本文将深入探讨如何创建自定义软键盘以及系统键盘的源码分析。 首先,让我们来看看自定义软键盘的实现过程。在Android中,我们...
本教程将深入探讨如何实现三种不同类型的Android自定义软键盘,并通过一个名为`mycustomkeyboard`的项目示例进行详细讲解。 首先,让我们了解自定义软键盘的基本原理。Android系统允许应用程序通过实现`...
本篇文章将详细介绍如何在Android中实现一个自定义软键盘,其中每个按键都可以设置不同的背景,并且键盘能自动适应布局,特别是当设备具有虚拟键盘时。 首先,创建一个新的XML布局文件,用于定义自定义键盘的按键。...