Android 总结:自定义键盘实现原理和三种实例详解,电子版已问世

本文详细介绍了Android自定义键盘的实现原理,包括KeyboardView的使用、按键定义、按键声音设置和处理按键输入的方法。通过实例展示了如何在XML中定义键盘布局,以及如何处理软键盘事件。同时,文章提供了代码示例来演示如何播放按键声音和处理不同键码的输入事件。
摘要由CSDN通过智能技术生成

<Key android:codes=“-5”

android:keyIcon=“@drawable/sym_keyboard_delete”/>

再监听就是:

if (primaryCode == Keyboard.KEYCODE_DELETE){}

这就表示,监听 delete 事件了!

定义 <Key/> 标签时常用到的 codes / KEYCODE 有:

public static final int KEYCODE_SHIFT = -1;

public static final int KEYCODE_MODE_CHANGE = -2;

public static final int KEYCODE_CANCEL = -3;

public static final int KEYCODE_DONE = -4;

public static final int KEYCODE_DELETE = -5;

public static final int KEYCODE_ALT = -6;

2. KeyboardView

处理绘制,检测按键,触摸动作:

  • A view that renders a virtual Keyboard. It handles rendering of keys and detecting key presses and touch movements.

我们会在Activity的xml文件中这样定义:

<android.inputmethodservice.KeyboardView

android:id=“@+id/keyboard_view”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_alignParentBottom=“true”

android:background=“@color/gray”

android:focusable=“true”

android:focusableInTouchMode=“true”

android:keyBackground=“@drawable/bg_keyboard_selector”

android:keyTextColor=“@color/keyTextColor”

android:visibility=“gone”

android:keyPreviewLayout=“@layout/key

  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值