在有些应用中会有定制软键盘的需求,往往实现起来会有些难度,或者说实现出来的效果不尽如人意。
最近在项目中有这种需求
博主也是不辱使命地完成了这个需求,效果图如下
说一下思路的和详细实现
主要是利用android自带的android.inputmethodservice.KeyboardView
对它进行自定义。
首先在res/目录下新建一个xml文件夹,新建一个xml文件
number.xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="33.33333%p" android:horizontalGap="1dp"
android:verticalGap="1dp" android:keyHeight="10%p">
<Row>
<Key android:codes="55" android:keyLabel="7" />
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="57" android:keyLabel="9" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="52" android:keyLabel="4" />
<Key android:codes="53" android:keyLabel="5" />
<Key android:codes="54" android:keyLabel="6" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="49" android:keyLabel="1" />
<Key android:codes="50" android:keyLabel="2" />
<Key android:codes="51" android:keyLabel="3" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="46" android:keyLabel="." />
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="-3" android:keyEdgeFlags="right" android:keyLabel="OK"/>
<!-- <Key android:codes="-3" android:keyEdgeFlags="right" android:keyIcon="@drawable/resale_btn_key_ok"/> -->
</Row>
</Keyboard>
键盘工具类
KeyboardUtil.java
public class KeyboardUtil {
private Context ctx;
private MyKeyboardView keyboardView;
private Keyboard k1;// 键盘
// private Keyboard k2;
// private Keyboard k3;
public boolean isnun = false;// 是否数据键盘