当前位置:  首页  >  PHP资讯  >  业界资讯

Android自定义软键盘的设计与实现代码

本篇文章主要介绍了Android自定义软键盘的设计与实现代码,有需要的可以了解一下。

偶然间发现了Android.inputmethodservice.Keyboard类,即android可以自定义键盘类,做了一个简单例子供大家参考。

效果如下:

先看界面布局文件

 <&#63;xml version="1.0" encoding="utf-8"&#63;>        

通过布局文件可以看出界面上有两个输入框,其中一个是密码输入框,界面上还有一个隐藏的键盘控件。

在res下新建xml文件夹,在xml文件夹中新建qwerty.xml和symbols.xml文件. qwerty.xml 是字母键盘布局,symbols.xml 是数字键盘布局,内如如下:

qwerty.xml内容

 <&#63;xml version="1.0" encoding="UTF-8"&#63;>                                            

symbols.xml 内容

 <&#63;xml version="1.0" encoding="utf-8"&#63;>                          

KeydemoActivity.java

 package cn.key; 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.java

 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() > 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; keyboardView.setKeyboard(k2); } } else if (primaryCode == 57419) { // go left if (start > 0) { ed.setSelection(start - 1); } } else if (primaryCode == 57421) { // go right if (start  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 isword(String str){ String wordstr = "abcdefghijklmnopqrstuvwxyz"; if (wordstr.indexOf(str.toLowerCase())>-1) { return true; } return false; } } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

内容推荐:免费高清PNG素材下载
吐了个 "CAO" !
扫码关注 PHP1 官方微信号
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社区 版权所有 京ICP备19059560号-4