最终效果:
实现这个自定义键盘的思路很简单:
1. 要写出一个数字键盘的布局;
2. 与 Edittext 结合使用,对每个按键的点击事件进行处理;
3. 禁用系统软键盘。
有了思路,实现起来就不难了。
1. 实现键盘的 xml 布局
网格样式的布局用 GridView 或者 RecyclerView 都可以实现,其实用 GridView 更方便一些,不过我为了多熟悉 RecyclerView 的用法,这里选择用了 RecyclerView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:background="@color/btn_gray"/>
<RelativeLayout
android:id="@+id/rl_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/iv_back_bg"
android:padding="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/keyboard_