安卓自定义安全键盘


Keyboard

我们只需要用到系统提供的两个类:Keyboard和KeyboardView。

res下新建xml,新建Keyboard文件

Keyboard中

android:keyWidth//每一个按键的宽
android:keyHeight//每一个按键的高
android:verticalGap//行与行之间的间隙
android:horizontalGap//列与列之间的间隙

Row的作用是按键的换行,每一个按键Key都在Row中。

Key中

android:keyLabel//按键上的文字
android:codes//输出的文字,对应ASCII表
android:keyIcon//按键放置图片
android:isRepeatable//删除键长按连续删除

宽高、间距的单位既可以是像素,英寸等,也可以是相对于基础取值的百分比,以%p结尾。

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    android:keyHeight="7.5%p"
    android:keyWidth="30%p">
    <Row android:verticalGap="1%p">
        <Key
            android:codes="49"
            android:keyLabel="1"
            android:horizontalGap="2%p">

        </Key>
        <Key
            android:codes="50"
            android:keyLabel="2"
            android:horizontalGap="2%p">

        </Key>
        <Key
            android:codes="51"
            android:keyLabel="3"
            android:horizontalGap="2%p">

        </Key>
    </Row>
    <Row android:verticalGap="1%p">
        <Key
            android:codes="52"
            android:keyLabel="4"
            android:horizontalGap="2%p">

        </Key>
        <Key
            android:codes="53"
            android:keyLabel="5"
            android:horizontalGap="2%p">

        </Key>
        <Key
            android:codes="54"
            android:keyLabel="6"
            android:horizontalGap="2%p">

        </Key>
    </Row>
    <Row android:verticalGap="1%p">
        <Key
            android:codes="55"
            android:keyLabel="7"
            android:horizontalGap="2%p">

        </Key>
        <Key
            android:codes="56"
            android:keyLabel="8"
            android:horizontalGap="2%p">

        </Key>
        <Key
            android:codes="57"
            android:keyLabel="9"
            android:horizontalGap="2%p">

        </Key>
    </Row>
    <Row>
        <Key
            android:codes="-2"
            android:keyLabel="abc"
            android:horizontalGap="2%p">

        </Key>
        <Key
            android:codes="48"
            android:keyLabel="0"
            android:horizontalGap="2%p">

        </Key>
        <Key
            android:codes="-5"
            android:isRepeatable="true"
            android:keyIcon="@mipmap/ic_delete"
            android:horizontalGap="2%p">

        </Key>
    </Row>
</Keyboard>

KeyboardView

KeyboardView

android:keyBackground//键盘背景图
android:keyPreviewLayout//键盘点击时候预览图的布局
android:keyPreviewHeight//键盘点击时候预览图的高度
android:keyTextColor//按键文字的颜色
android:keyTextSize//按键文字大小
android:labelTextSize//如果同时设置了文字+图片的按键
android:shadowColor 
android:shadowRadius

代码

<android.inputmethodservice.KeyboardView
    android:id="@+id/kbv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#999999"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:keyBackground="@color/colorPrimaryDark"
    android:keyPreviewHeight="64dp"
    android:keyPreviewLayout="@layout/keyboard_preview"
    android:keyTextColor="@android:color/black"
    android:keyTextSize="24sp"
    android:labelTextSize="18sp"
    android:paddingTop="8dip"
    android:paddingBottom="8dip"
    android:shadowColor="#FFFFFF"
    android:shadowRadius="0.0"
    android:visibility="gone"
    />

使用

设置

Keyboard keyboard = new Keyboard(this,R.xml.tea_keyboard_num);
keyboardView.setKeyboard(keyboard);
keyboardView.setPreviewEnabled(true);
keyboardView.setOnKeyboardActionListener(onKeyboardActionListener);

OnKeyboardActionListener

KeyboardView.OnKeyboardActionListener onKeyboardActionListener = new KeyboardView.OnKeyboardActionListener() {
    ......
    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成

        } else if (primaryCode == Keyboard.KEYCODE_DELETE) {//回退
            Toast.makeText(BoardActivity.this,"delete",Toast.LENGTH_SHORT).show();
        } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {//大小写切换

        } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {//数字键盘切换

        } else {

        }
    }
};

参考

https://github.com/onlyloveyd/LazyKeyboard

https://github.com/r17171709/android_demo/tree/master/KeyBoardDemo


文章作者:
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 !
  目录