移步新博客 https://blog.csdn.net/SValence/article/details/93901651
源码地址:GitHub: https://github.com/SValence/SafeKeyboard
在一些比较敏感的输入场合,我们不能调用系统的软键盘进行输入信息,这时候就需要自定义一个软键盘来完成输入工作,由此就需要下面 Android 安全软键盘开发 的工作,本文主要从项目需求出发,梳理开发流程,并给出一个实际开发的例子作为参考。
一、Android 安全软键盘开发流程
1.建立软键盘样式
即在项目res文件夹中创建所需要的各种软键盘的样式(比如说:字母、数字、符号 等)。
2.创建layout布局文件
在布局文件中给软键盘创建container,以便显示软键盘
3.自定义KeyboardView
自定义一个KeyboardView 并继承自KeyboardView,在自定义的KeyboardView中绘制特殊按键,包括按键的点击背景,图片,文字 等。
4.自定义一个普通java类,一般取名为 **Keyboard.java
把软键盘加载到container中,即在布局文件里预留的存放软键盘 的container。
在类的内部实现软键盘的输入控制,键盘转换控制,软键盘的显示与隐藏控制 等。
在需要用到软键盘的Activity中实例化该Keyboard 类,并传入必要的数据和信息。
先上一个效果图:
二、Android 安全软键盘开发实例(仅作参考)
1.创建项目后, 在layout布局文件中设计基本布局,参考如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/normalEditText"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_marginEnd="10sp"
android:layout_marginStart="10sp"
android:hint="@string/keyboard_system"
android:inputType="text" />
<EditText
android:id="@+id/safeEditText"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_marginEnd="10sp"
android:layout_marginStart="10sp"
android:hint="@string/keyboard_demo_new"
android:inputType="textVisiblePassword" />
</LinearLayout>
<LinearLayout
android:id="@+id/keyboardViewPlace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" />
</RelativeLayout>
这里使用一个线性布局作为存放自定义软键盘的容器,说明一下,一般使用到自定义软键盘的页面的布局不会很复杂,无非就是想要在输入关键数据比如密码时才使用到软键盘,而软键盘一般都是在手机等移动设备屏幕的最下方显示,以方便输入,所以这里使用了RelativeLayout方便显示,当然也可以使用现在google推行的ConstraintLayout布局来做,这个根据个人喜好。所以在原有的项目里加入一个存放软键盘的容器,也不会给原有的项目带来很大的麻烦或者影响,所以这种做法,我个人认为还是可取的.
3. 在项目res文件夹下创建xml文件夹,并创建出自己需要的软键盘布局,比如说:数字,字母,符号等,一般来说这三个已经足够用了下面是三个例子
3.1 字母布局文件 keyboard_letter.xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="1%p"
android:keyHeight="@dimen/key_height"
android:keyWidth="10%p"
android:verticalGap="@dimen/key_vertical_gap">
<Row>
<Key android:codes="113" android:keyEdgeFlags="left" android:keyLabel="q" android:keyWidth="8.9%p" />
<Key android:codes="119" android:keyLabel="w" android:keyWidth="8.9%p" />
<Key android:codes="101" android:keyLabel="e" android:keyWidth="8.9%p" />
<Key android:codes="114" android:keyLabel="r" android:keyWidth="8.9%p" />
<Key android:codes="116" android:keyLabel="t" android:keyWidth="8.9%p" />
<Key android:codes="121" android:keyLabel="y" android:keyWidth="8.9%p" />
<Key android:codes="117" android:keyLabel="u" android:keyWidth="8.9%p" />
<Key android:codes="105" android:keyLabel="i" android:keyWidth="8.9%p" />
<Key android:codes="111" android:keyLabel="o" android:keyWidth="8.9%p" />
<Key android:codes="112" android:keyEdgeFlags="right" android:keyLabel="p" android:keyWidth="8.9%p" />
</Row>
<Row>
<Key android:codes="97" android:horizontalGap="5.5%p" android:keyEdgeFlags="left" android:keyLabel="a" android:keyWidth="9%p" />
<Key android:codes="115" android:keyLabel="s" android:keyWidth="9%p" />
<Key android:codes="100" android:keyLabel="d" android:keyWidth="9%p" />
<Key android:codes="102" android:keyLabel="f" android:keyWidth="9%p" />
<Key android:codes="103" android:keyLabel="g" android:keyWidth="9%p" />
<Key android:codes="104" android:keyLabel="h" android:keyWidth="9%p" />
<Key android:codes="106" android:keyLabel="j" android:keyWidth="9%p" />
<Key android:codes="107" android:keyLabel="k" android:keyWidth="9%p" />
<Key android:codes="108" android:keyEdgeFlags="right" android:keyLabel="l" android:keyWidth="9%p" />
</Row>
<Row>
<Key android:codes="-1" android:isModifier="true" android:isSticky="true" android:keyEdgeFlags="left" android:keyWidth="13%p" />
<Key android:codes="122" android:horizontalGap="1.5%p" android:keyLabel="z" android:keyWidth="9%p" />
<Key android:codes="120" android:keyLabel="x" android:keyWidth="9%p" />
<Key android:codes="99" android:keyLabel="c" android:keyWidth="9%p" />
<Key android:codes="118" android:keyLabel="v" android:keyWidth="9%p" />
<Key android:codes="98" android:keyLabel="b" android:keyWidth="9%p" />
<Key android:codes="110" android:keyLabel="n" android:keyWidth="9%p" />
<Key android:codes="109" android:keyLabel="m" android:keyWidth="9%p" />
<Key android:codes="-5" android:horizontalGap="1.5%p" android:isRepeatable="true" android:keyWidth="13%p" />
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="-2" android:keyLabel="123" android:keyWidth="19%p" />
<Key android:codes="32" android:isRepeatable="false" android:keyLabel="space" android:keyWidth="58%p" />
<Key android:codes="100860" android:keyEdgeFlags="right" android:keyLabel="#+=" android:keyWidth="19%p" />
</Row>
</Keyboard>
3.2 字符键盘布局文件 keyboard_symbol.xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard
xmlns:android="http://schemas.android.com/apk/res/android"
android:keyHeight="@dimen/key_num_height"
android:keyWidth="10%p"
android:horizontalGap="1%p"
android:verticalGap="@dimen/key_vertical_gap">
<Row>
<Key android:keyWidth="8.9%p" android:codes="33" android:keyLabel="!" android:keyEdgeFlags="left" />
<Key android:keyWidth="8.9%p" android:codes="64" android:keyLabel="@string/keyboard_key1" /><!--@-->
<Key android:keyWidth="8.9%p" android:codes="35" android:keyLabel="#" />
<Key android:keyWidth="8.9%p" android:codes="36" android:keyLabel="$" />
<Key android:keyWidth="8.9%p" android:codes="37" android:keyLabel="%" />
<Key android:keyWidth="8.9%p" android:codes="94" android:keyLabel="^" />
<Key android:keyWidth="8.9%p" android:codes="38" android:keyLabel="@string/keyboard_key2" /><!--&-->
<Key android:keyWidth="8.9%p" android:codes="42" android:keyLabel="*" />
<Key android:keyWidth="8.9%p" android:codes="40" android:keyLabel="(" />
<Key android:keyWidth="8.9%p" android:codes=