package com.example.yangkexue.myapplication;
/**
* →
* author : yangkexue
* time : 2017/5/3
* 简书博客 : http://www.jianshu.com/u/eb77504b1d68
*/
import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Build;
import android.text.Editable;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import java.lang.reflect.Method;
import java.util.List;
public class KeyboardViewS implements OnKeyboardActionListener {
private static final int KEYBOARD_DURATION = 350;
private EditText mEditText;
private Keyboard mKeyboard;
/** 是否大写 */
private boolean isUperCase = false;
private android.inputmethodservice.KeyboardView keyboardView;
public KeyboardViewS(Activity act, EditText edit) {
this.mEditText = edit;
mKeyboard = new Keyboard(act, R.xml.qwerty);
keyboardView = (android.inputmethodservice.KeyboardView) (act.findViewById(R.id.keyboard_view));
keyboardView.setKeyboard(mKeyboard);
/** 设置没有弹窗的提示 */
keyboardView.setPreviewEnabled(false);
keyboardView.setOnKeyboardActionListener(this);
uperCase();
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = mEditText.getText();
int start = mEditText.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) { // 完成
hideKeyboard(keyboardView);
} 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) {// 大小写切换
uperCase();
keyboardView.setKeyboard(mKeyboard);
} else if (primaryCode == 57419) {
if (start > 0) {
mEditText.setSelection(start - 1);
}
} else if (primaryCode == 57421) {
if (start < mEditText.length()) {
mEditText.setSelection(start + 1);
}
} else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
/** 键盘大小写切换 */
private void uperCase() {
final List<Key> keylist = mKeyboard.getKeys();
if (isUperCase) {
isUperCase = 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 {
isUperCase = 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 static void setSystemInputGone(EditText editText) {
if (Build.VERSION.SDK_INT >= 11) {
Class<EditText> cls = EditText.class;
try {
Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setShowSoftInputOnFocus.setAccessible(false);
setShowSoftInputOnFocus.invoke(editText, false);
} catch (Exception e) {
e.printStackTrace();
}
} else {
editText.setInputType(android.text.InputType.TYPE_NULL);
editText.setInputType(editText.getInputType());
}
}
/** 显示键盘 */
public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
Animation anim = AnimationUtils.translateAnimationOut(KEYBOARD_DURATION);
keyboardView.setVisibility(View.VISIBLE);
keyboardView.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
super.onAnimationEnd(animation);
keyboardView.clearAnimation();
}
});
}
}
/** 隐藏键盘 */
public static void hideKeyboard(final KeyboardView keyboardView) {
if (keyboardView == null) {
return;
}
if (keyboardView.getVisibility() == View.VISIBLE) {
Animation anim = AnimationUtils.translateAnimationIn(KEYBOARD_DURATION);
keyboardView.startAnimation(anim);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
super.onAnimationEnd(animation);
keyboardView.clearAnimation();
}
});
keyboardView.setVisibility(View.GONE);
}
}
private boolean isword(String str) {
final String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase()) > -1) {
return true;
}
return false;
}
@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) {}
}
没有合适的资源?快使用搜索试试~ 我知道了~
自定义 android 键盘
共91个文件
xml:45个
ds_store:15个
bin:5个
5星 · 超过95%的资源 需积分: 13 70 下载量 173 浏览量
2017-05-03
16:51:48
上传
评论 1
收藏 856KB ZIP 举报
温馨提示
一个使用 Keyboard + KeyboardView 自定义的英文全键盘,支持大小写,连续删除 详情可以看这里:http://www.jianshu.com/p/325a7efd5d0d
资源推荐
资源详情
资源评论
收起资源包目录
MyApplication.zip (91个子文件)
MyApplication
.gitignore 118B
app
.gitignore 7B
build.gradle 952B
src
.DS_Store 6KB
main
AndroidManifest.xml 876B
res
anim
anim_slide_out.xml 327B
anim_slide_in.xml 325B
layout
activity_main.xml 1KB
drawable
sel_keyboard.xml 265B
keyboard_bc_s.xml 247B
keyboard_bc_n.xml 560B
.DS_Store 6KB
xml
qwerty.xml 3KB
mipmap-hdpi
ic_launcher_round.png 4KB
ic_launcher.png 3KB
drawable-xhdpi
shift.png 1KB
keyboard_delete.png 2KB
values
strings.xml 77B
animation_default_duration.xml 177B
colors.xml 385B
styles.xml 383B
.DS_Store 6KB
java
com
.DS_Store 6KB
example
.DS_Store 6KB
yangkexue
.DS_Store 6KB
myapplication
MainActivity.java 855B
AnimationUtils.java 1KB
readme 67B
KeyboardViewS.java 6KB
AnimationListener.java 517B
.DS_Store 6KB
.DS_Store 6KB
libs
proguard-rules.pro 938B
app.iml 11KB
settings.gradle 15B
build.gradle 498B
gradlew 5KB
.DS_Store 6KB
MyApplication.iml 867B
gradlew.bat 2KB
gradle.properties 730B
.gradle
.DS_Store 6KB
3.3
.DS_Store 6KB
taskArtifacts
fileSnapshots.bin 4.35MB
taskArtifacts.lock 17B
fileHashes.bin 276KB
taskArtifacts.bin 130KB
tasks
.DS_Store 6KB
_app_compileDebugJavaWithJavac
localClassSetAnalysis
localClassSetAnalysis.lock 17B
localClassSetAnalysis.bin 52KB
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 20KB
localJarClasspathSnapshot.lock 17B
.DS_Store 6KB
.idea
misc.xml 2KB
libraries
exposed_instrumentation_api_publish_0_5.xml 636B
hamcrest_library_1_3.xml 334B
constraint_layout_solver_1_0_2.xml 346B
javawriter_2_1_1.xml 322B
constraint_layout_1_0_2.xml 401B
support_compat_25_3_1.xml 577B
jsr305_2_0_1.xml 322B
support_fragment_25_3_1.xml 583B
javax_inject_1.xml 316B
support_v4_25_3_1.xml 395B
support_core_ui_25_3_1.xml 580B
espresso_idling_resource_2_2_2.xml 618B
support_annotations_25_3_1.xml 519B
junit_4_12.xml 467B
animated_vector_drawable_25_3_1.xml 607B
rules_0_5.xml 546B
javax_annotation_api_1_2.xml 350B
runner_0_5.xml 549B
hamcrest_core_1_3.xml 516B
hamcrest_integration_1_3.xml 346B
espresso_core_2_2_2.xml 585B
support_vector_drawable_25_3_1.xml 604B
support_media_compat_25_3_1.xml 595B
support_core_utils_25_3_1.xml 589B
appcompat_v7_25_3_1.xml 571B
workspace.xml 180KB
gradle.xml 626B
vcs.xml 167B
copyright
profiles_settings.xml 74B
.DS_Store 6KB
runConfigurations.xml 564B
compiler.xml 686B
modules.xml 363B
gradle
wrapper
gradle-wrapper.properties 230B
gradle-wrapper.jar 52KB
.DS_Store 6KB
local.properties 438B
共 91 条
- 1
资源评论
- 申海2018-06-20可以的,以后有用
- anshuiyin2019-10-24好用的,提取一部分代码用
狂奔的蜗牛_杨柯
- 粉丝: 29
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功