android 自定义车牌键盘(kotlin)

前言

平时停车缴费都要填车牌号码,就想着自己能不能也做个车牌键盘demo。
自定义车牌键盘能满足(普通车牌,新能源,警车,军车,领事馆车,教练车以及特种车辆等车牌)

示例图

在这里插入图片描述
在这里插入图片描述

实现需求

1、车牌前两位默认是:粤A

2、第一个控件,默认是省份键盘

3、剩下控件,默认是abc键盘

4、当前输入框输完后能自动跳转到下个控件

5、当前输入框有选中背景,其他的没有

定义键盘文件

在res文件夹下创建xml文件夹,创建省份键盘(province.xml)和abc键盘(abc.xml)

province.xml

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="1%p"
    android:keyWidth="10%p"
    android:keyHeight="8%p"
    android:verticalGap="0.0px">
    <Row android:verticalGap="1%p">
        <Key
            android:codes="20140"
            android:keyLabel="" />
        <Key
            android:codes="27941"
            android:keyLabel="" />
        <Key
            android:codes="20864"
            android:keyLabel="" />
        <Key
            android:codes="40065"
            android:keyLabel="" />
        <Key
            android:codes="26187"
            android:keyLabel="" />
        <Key
            android:codes="33945"
            android:keyLabel="" />
        <Key
            android:codes="36797"
            android:keyLabel="" />
        <Key
            android:codes="21513"
            android:keyLabel="" />
        <Key
            android:codes="40657"
            android:keyLabel="" />
        <Key
            android:codes="27818"
            android:keyLabel="" />
    </Row>
    <Row android:verticalGap="1%p">
        <Key
            android:codes="33487"
            android:keyLabel="" />
        <Key
            android:codes="27993"
            android:keyLabel="" />
        <Key
            android:codes="30358"
            android:keyLabel="" />
        <Key
            android:codes="38397"
            android:keyLabel="" />
        <Key
            android:codes="36195"
            android:keyLabel="" />
        <Key
            android:codes="35947"
            android:keyLabel="" />
        <Key
            android:codes="37122"
            android:keyLabel="" />
        <Key
            android:codes="28248"
            android:keyLabel="" />
        <Key
            android:codes="31908"
            android:keyLabel="" />
        <Key
            android:codes="26690"
            android:keyLabel="" />
    </Row>
    <Row android:verticalGap="1%p">
        <Key
            android:codes="28189"
            android:keyLabel="" />
        <Key
            android:codes="24029"
            android:keyLabel="" />
        <Key
            android:codes="36149"
            android:keyLabel="" />
        <Key
            android:codes="20113"
            android:keyLabel="" />
        <Key
            android:codes="34255"
            android:keyLabel="" />
        <Key
            android:codes="38485"
            android:keyLabel="" />
        <Key
            android:codes="29976"
            android:keyLabel="" />
        <Key
            android:codes="38738"
            android:keyLabel="" />
        <Key
            android:codes="29756"
            android:keyLabel="" />
        <Key
            android:codes="26032"
            android:keyLabel="" />
    </Row>
    <Row>
        <Key
            android:codes="-1"
            android:isModifier="true"
            android:isSticky="true"
            android:keyWidth="10%p"
            android:keyLabel="ABC" />
        <Key
            android:codes="23425"
            android:keyLabel="" />
        <Key
            android:codes="20351"
            android:keyLabel="使" />
        <Key
            android:codes="39046"
            android:keyLabel="" />
        <Key
            android:codes="35686"
            android:keyLabel="" />
        <Key
            android:codes="23398"
            
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Kotlin 中,可以通过以下步骤创建自定义 Toast: 1. 创建一个 XML 布局文件来定义 Toast 的外观。例如,可以创建一个名为 custom_toast.xml 的文件,并在其中定义 Toast 的布局,例如: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#DAA520" android:orientation="horizontal" android:padding="16dp"> <ImageView android:id="@+id/toast_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/toast_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is a custom toast" android:textColor="#FFFFFF" android:textSize="16sp" /> </LinearLayout> ``` 2. 在需要显示 Toast 的代码中,使用 LayoutInflater 加载布局,并将其设置为 Toast 的视图。例如: ``` val inflater = LayoutInflater.from(context) val layout = inflater.inflate(R.layout.custom_toast, null) val toast = Toast(context) toast.duration = Toast.LENGTH_SHORT toast.view = layout toast.show() ``` 3. 可以使用 findViewById() 方法从布局中检索视图,并对它们进行操作。例如: ``` val toastIcon = layout.findViewById<ImageView>(R.id.toast_icon) toastIcon.setImageResource(R.drawable.ic_custom_icon) val toastText = layout.findViewById<TextView>(R.id.toast_text) toastText.text = "This is a custom toast message" ``` 完整的示例代码: ``` // Inflate the layout for the toast val inflater = LayoutInflater.from(context) val layout = inflater.inflate(R.layout.custom_toast, null) // Retrieve the views from the layout val toastIcon = layout.findViewById<ImageView>(R.id.toast_icon) val toastText = layout.findViewById<TextView>(R.id.toast_text) // Customize the views toastIcon.setImageResource(R.drawable.ic_custom_icon) toastText.text = "This is a custom toast message" // Create and show the toast val toast = Toast(context) toast.duration = Toast.LENGTH_SHORT toast.view = layout toast.show() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值