Android 总结:自定义键盘实现原理和三种实例详解

本文详细介绍了Android自定义键盘的实现原理,包括KeyboardView的使用、按键定义、按键声音的设置以及如何处理按键输入。通过分析键盘布局XML,展示了如何定义每个键的属性,如文本、Unicode值和行为。此外,还探讨了如何通过继承EditText自定义视图实现键盘,并确保键盘在显示时不遮挡输入框。文中提供了几个实例,包括如何生成随机数的键以及如何处理不同的按键事件,如删除、换行和完成输入。
摘要由CSDN通过智能技术生成

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_preview_layout”

/>

  • 其中 android:keyPreviewLayout 指的是每当我们点击键盘上的某个键时,短暂弹出的布局。

| 属性名 | 描述 |

| — | — |

| android:keyBackground | Image for the key. |

| android:keyPreviewLayout | Layout resource for key press feedback. |

| android:keyPreviewOffset | Vertical offset of the key press feedback from the key. |

| android:keyTextColor | Color to use for the label in a key. |

| android:keyTextSize | Size of the text for character keys. |

| android:labelTextSize | Size of the text for custom keys with some text and no icon. |

| android:popupLayout | Layout resource for popup keyboards. |

| android:verticalCorrection | Amount to offset the touch Y coordinate by, for bias correction. |

  • 我们需要建一个xml文件,来布局我们的视图,

一般是在res文件夹中建一个名为xml的文件夹,在里面新建立一个xml布局文件。

  • 每一个按键的属性主要包括

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值