Android自定义软键盘KeyboardView 使用实例

本文介绍了如何在Android应用中自定义软键盘,通过利用android.inputmethodservice.KeyboardView,详细阐述了键盘布局的设计、XML配置、键盘工具类的编写以及在Activity中的初始化。在实现过程中,博主针对退格键和OK键的特殊处理进行了说明,并提供了demo的下载链接。
摘要由CSDN通过智能技术生成

在有些应用中会有定制软键盘的需求,往往实现起来会有些难度,或者说实现出来的效果不尽如人意。
最近在项目中有这种需求
这里写图片描述
博主也是不辱使命地完成了这个需求,效果图如下
这里写图片描述
说一下思路的和详细实现

主要是利用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;// 是否数据键盘
    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值