Android自定义键盘

来源:互联网 发布:调查问卷怎么录数据 编辑:程序博客网 时间:2024/04/28 22:42
当我们在项目中难免会发现Android自带的键盘输入法不能满足我们的业务需示时,我们需要对键盘进行改造,让他更好的服务于我们的项目,下面的关于自定义数据键盘来记录键盘的开发过程。

首先我们需要一个键盘的定义文件:res/xml/digit.xml, 该文件用来定义你的键盘布局

<?xml version="1.0" encoding="utf-8"?><Keyboard xmlns:android="http://schemas.android.com/apk/res/android"    android:keyWidth="25%p" android:keyHeight="50dp"    android:horizontalGap="0px" android:verticalGap="0px">    <Row>        <Key android:codes="49" android:keyLabel="1"></Key>        <Key android:codes="50" android:keyLabel="2"></Key>        <Key android:codes="51" android:keyLabel="3"></Key>        <Key android:codes="152" android:keyLabel="清除" android:keyHeight="100dp"></Key>    </Row>    <Row>        <Key android:codes="52" android:keyLabel="4"></Key>        <Key android:codes="53" android:keyLabel="5"></Key>        <Key android:codes="54" android:keyLabel="6"></Key>    </Row>    <Row>        <Key android:codes="55" android:keyLabel="7"></Key>        <Key android:codes="56" android:keyLabel="8"></Key>        <Key android:codes="57" android:keyLabel="9"></Key>        <Key android:codes="-4" android:keyLabel="确定" android:keyHeight="100dp"></Key>    </Row>    <Row>        <Key android:codes="46" android:keyLabel="."></Key>        <Key android:codes="48" android:keyLabel="0"></Key>        <Key android:codes="-5" android:keyLabel="删除"></Key>    </Row></Keyboard>

其中

Row:表示每一行的布局属性

Key:每一个键的定义

codes:键的标识符

keyLabel:键上显示的字符

keyHeight:键的高度


定义键盘控件:

<android.inputmethodservice.KeyboardView        android:id="@+id/keyboard"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/white"        android:shadowColor="@color/white"        android:focusable="true"        android:focusableInTouchMode="true"        android:keyBackground="@drawable/bord"        android:layout_alignParentBottom="true"        android:keyTextColor="@color/black"        android:visibility="gone"        />

KeyBoardView就是Android专门为键盘定义的一个控件,我们可以把上面定义的键盘布局与该控件关联来显示:

        keyboardView = (KeyboardView) findViewById(R.id.keyboard);        keyboard = new Keyboard(this, R.xml.digit);        keyboardView.setKeyboard(keyboard);        keyboardView.setPreviewEnabled(false);//不显示预览提示        keyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {            @Override            public void onPress(int primaryCode) {            }            @Override            public void onRelease(int primaryCode) {            }            @Override            public void onKey(int primaryCode, int[] keyCodes) {                Editable editable = editText.getText();                int start = editText.getSelectionStart();                if(primaryCode == Keyboard.KEYCODE_DONE){                    hideKeyBoard();                }else if(primaryCode == Keyboard.KEYCODE_DELETE){                    if(editable != null && editable.length()>0){                        if(start>0){                            editable.delete(start-1,start);                        }                    }                }else if(primaryCode == 152){                    editable.clear();                }else{                    editable.insert(start, Character.toString((char)primaryCode));                }            }            @Override            public void onText(CharSequence text) {            }            @Override            public void swipeLeft() {            }            @Override            public void swipeRight() {            }            @Override            public void swipeDown() {            }            @Override            public void swipeUp() {            }        });

其中

KeyboardView.OnKeyboardActionListener()

回调函数就是用来监听你的按键:当你按下某个键后,在digit.xml中定义的codes就会作为参数传入,primaryCode就是该codes,这时候你就可以随意发挥你的想像,在我们的代码中实现你的梦想吧。

另外,当你在EditText中使用时,不免会自动带出系统内置的输入法,可以通过设置editText.setInputType(InputType.TYPE_NULL);来屏蔽它

通过控制keyBoardView的显示属性来控制键盘的显示与隐藏。

至此我们的键盘基本就完成了。
0 1