Android提供KeyboardView可以让用户实现对软键盘的自定义;具体的定义数字键盘,还是字符键盘可以按照自己的需求进行定制;
本文提供数字键盘的自定义实现,并解决软键盘按下后review背景按钮样式的修改;
(1)main.xml文件下面定义一个KeyboardView;
(2)在xml文件夹下面定义一个数字键盘number.xml文件;
(3)提供一个软件盘的实现工具类
先上运行后的效果图,不是你要的就不用仔细读代码啦
main.xml中的核心代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lyy.input.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="31dp"
android:ems="10"
android:inputType="number" >
</EditText>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboard_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#F4F4F4"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="@drawable/btn_keyboard_key"
android:keyPreviewLayout="@layout/key_preview_layout"
android:shadowColor="#FFFFFF"
android:shadowRadius="0.0"
android:keyTextColor="#000000" />
</RelativeLayout>
</RelativeLayout>
number.xml中的代码如下;主要用来定义数字键;注意该文件存放在xml文件夹下面;
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="3dp"
android:keyHeight="48dp"
android:keyWidth="33.33%p"
android:verticalGap="3dp" >
<Row>
<Key
android:codes="49"
android:keyLabel="1" />
<Key
android:codes="50"
android:keyLabel="2" />
<Key
android:codes="51"
andr