先看界面布局文件

<?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" ><EditTextandroid:id="@+id/edit"android:layout_width="fill_parent"android:layout_height="wrap_content" /><EditTextandroid:id="@+id/edit1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:password="true" /><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><android.inputmethodservice.KeyboardViewandroid: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:visibility="gone" /></RelativeLayout></LinearLayout>

通过布局文件可以看出界面上有两个输入框,其中一个是密码输入框,界面上还有一个隐藏的键盘控件。
在res下新建xml文件夹,在xml文件夹中新建qwerty.xml和symbols.xml文件. qwerty.xml 是字母键盘布局,symbols.xml 是数字键盘布局,内如如下

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="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:horizontalGap="4.999995%p" android:codes="97"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: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="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: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="44"android:keyLabel="," /><Key android:keyWidth="29.999996%p" android:codes="32"android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" /><Key android:keyWidth="14.999998%p" android:codes="46"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="49" android:keyLabel="1" /><Key android:codes="50" android:keyLabel="2" /><Key android:codes="51" android:keyLabel="3" /><Key android:codes="57419" android:keyEdgeFlags="right"android:keyIcon="@drawable/sym_keyboard_left" /></Row><Row><Key android:codes="52" android:keyLabel="4" /><Key android:codes="53" android:keyLabel="5" /><Key android:codes="54" android:keyLabel="6" /><Key android:codes="57421" android:keyEdgeFlags="right"android:keyIcon="@drawable/sym_keyboard_right" /></Row><Row><Key android:codes="55" android:keyLabel="7" /><Key android:codes="56" android:keyLabel="8" /><Key android:codes="57" android:keyLabel="9" /><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="48" android:keyLabel="0" /><Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" /></Row>
</Keyboard>

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;@Overridepublic 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() {@Overridepublic boolean onTouch(View v, MotionEvent event) {new KeyboardUtil(act, ctx, edit).showKeyboard();return false;}});edit1.setOnTouchListener(new OnTouchListener() {@Overridepublic 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() {@Overridepublic void swipeUp() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeDown() {}@Overridepublic void onText(CharSequence text) {}@Overridepublic void onRelease(int primaryCode) {}@Overridepublic void onPress(int primaryCode) {}@Overridepublic 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 leftif (start > 0) {ed.setSelection(start - 1);}} else if (primaryCode == 57421) { // go rightif (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 isword(String str){String wordstr = "abcdefghijklmnopqrstuvwxyz";if (wordstr.indexOf(str.toLowerCase())>-1) {return true;}return false;}}

源码下载地址:http://download.csdn.net/detail/hfsu0419/4534209

Android自定义软键盘的实现相关推荐

  1. android自定义系统键盘,Android自定义软键盘

    [实例简介] Android自定义软键盘 [实例截图] [核心代码] keydemo └── keydemo ├── AndroidManifest.xml ├── bin │   ├── class ...

  2. Android 自定义软键盘实现 数字九宫格

    前言 最近项目在对接美团外卖功能 实现外面小哥凭取货码取货 对接完功能后 用户反馈 弹出的软键盘 很难输入 数字太小了 大概是下面这种显示方式 需求 组长说 要不搞一个自定义软键盘吧 数字搞大点 方便 ...

  3. Android自定义软键盘输入法,隐藏系统输入法显示光标的实现

    android实现自定义软键盘,先上图看效果,效果基本上是仿ios输入法实现的 这里是实现隐藏系统输入法,同时让EditText能获取光标的代码部分(通过反射调用): <span style=& ...

  4. Android自定义软键盘样式:字母、数字、标点三种切换

    先看效果图: 1.在需要的调用软键盘的activity_mian.xml中加入键盘控件 <!--自定义键盘控件--> <RelativeLayoutandroid:layout_wi ...

  5. android自动软键盘,Android自定义软键盘

    MyKeyboard Android自定义键盘的使用 实现步骤 第一步: 1.新建一个xml文件夹放在res目录下面,然后新建xml文件:money_keyboard.xml 2.然后在XML文件中添 ...

  6. Android 自定义软键盘实现 数字九宫格,2021年Android常见面试题目

    实现效果GIF 实现代码 自定义View 一个NineNumericKeyboardView /** Author by Lyu Date on 2021/5/26-19:55 Description ...

  7. Android 自定义软键盘实现

    module链接:https://download.csdn.net/download/meixi_android/10652565 compile project(':edlibrary') , ' ...

  8. android 自定义软键盘

    哦然间发现了android.inputmethodservice.Keyboard类,即android可以自定义键盘类,做了一个简单例子供大家参考, 首先看看效果图: 键盘内容布局:keyconten ...

  9. android自定义键盘开源,Android自定义软键盘的设计与实现代码

    偶然间发现了Android.inputmethodservice.Keyboard类,即android可以自定义键盘类,做了一个简单例子供大家参考. 效果如下: 先看界面布局文件 android:la ...

最新文章

  1. RHEL6.3安装vsftpd
  2. 想要学习Python爬虫,看这几本书就够了
  3. C语言基础学习day10
  4. [转载]js节流与防抖,防止重复提交、防止频繁重复点击
  5. JDK1.8中的HashMap,HashTable,ConcurrentHashMap有什么区别?
  6. RecyclerView列表控件漂亮时间线实现
  7. RPC和Restful深入理解
  8. Hibernate的几个关键类的详解及Hibernate的运行过程
  9. IntelliJ IDEA 连接数据库 详细过程
  10. 远程办公首日企业通讯软件崩溃、紧急扩容,云办公怎么了?
  11. pandas颠倒dataframe与series的顺序
  12. c++ java setobjectarrayelement_java中jni的使用:C/C++操作java中的数组
  13. php 应用时间,PHP 日期与时间
  14. 哎呀,人家不小心变油腻了呢
  15. 利用URL对网络资源进行下载(简制版)
  16. 行亦谦ACM自闭之旅第七周
  17. opencv位运算,cv2.bitwise_and,cv2.bitwise_or,cv2.bitwise_not,cv2.bitwise_xor
  18. 试验数据管理系统TDM与SDM
  19. Kurento-6.7.1 媒体服务器搭建详细教程(Kurento-Media-Server)
  20. 在线版音乐播放器APP

热门文章

  1. 第七届力学、数学与应用物理学国际会议(ICMMAP 2023)
  2. 对于PM来说:拥有PMP证书,就拥有更多机会
  3. System Toolkit for Mac(mac系统维护软件)
  4. IOS正弦Sin,余弦Cos,正切Tan的计算
  5. 建筑施工技术【10】
  6. nvidia深度学习加速库apex简单介绍
  7. qt 下 原生socket tcp 基本用法
  8. win10 家庭版如何能远程桌面控制
  9. java MongoDB直接存pojo类
  10. unity3d控制物体移动