当前位置:网站首页>一款简易的自定义数字键盘输入控件
一款简易的自定义数字键盘输入控件
2022-04-22 09:42:00 【建人尹口】
实际效果如下:

首先布局layout文件,采用三总四横等比的方式进行布局:
<?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="match_parent"
android:orientation="vertical"
android:paddingStart="@dimen/dp_30"
android:paddingEnd="@dimen/dp_30"
android:paddingTop="@dimen/dp_20"
android:paddingBottom="@dimen/dp_20"
android:clickable="true"
android:background="@drawable/concise_keyboard_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:gravity="center">
<Button
android:id="@+id/btn_of_1"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="1"
android:stateListAnimator="@null"
android:textColor="@color/text_first_level_color"
android:textSize="@dimen/sp_28"
android:background="@drawable/keyboard_btn_bg_selector"/>
<Button
android:id="@+id/btn_of_2"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="2"
android:stateListAnimator="@null"
android:layout_marginStart="@dimen/dp_50"
android:textColor="@color/text_first_level_color"
android:textSize="@dimen/sp_28"
android:background="@drawable/keyboard_btn_bg_selector"/>
<Button
android:id="@+id/btn_of_3"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="3"
android:stateListAnimator="@null"
android:layout_marginStart="@dimen/dp_50"
android:textColor="@color/text_first_level_color"
android:textSize="@dimen/sp_28"
android:background="@drawable/keyboard_btn_bg_selector"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_marginTop="@dimen/dp_14"
android:gravity="center">
<Button
android:id="@+id/btn_of_4"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="4"
android:stateListAnimator="@null"
android:textColor="@color/text_first_level_color"
android:textSize="@dimen/sp_28"
android:background="@drawable/keyboard_btn_bg_selector"/>
<Button
android:id="@+id/btn_of_5"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="5"
android:stateListAnimator="@null"
android:layout_marginStart="@dimen/dp_50"
android:textColor="@color/text_first_level_color"
android:textSize="@dimen/sp_28"
android:background="@drawable/keyboard_btn_bg_selector"/>
<Button
android:id="@+id/btn_of_6"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="6"
android:stateListAnimator="@null"
android:layout_marginStart="@dimen/dp_50"
android:textColor="@color/text_first_level_color"
android:textSize="@dimen/sp_28"
android:background="@drawable/keyboard_btn_bg_selector"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_marginTop="@dimen/dp_14"
android:gravity="center">
<Button
android:id="@+id/btn_of_7"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="7"
android:stateListAnimator="@null"
android:textColor="@color/text_first_level_color"
android:textSize="@dimen/sp_28"
android:background="@drawable/keyboard_btn_bg_selector"/>
<Button
android:id="@+id/btn_of_8"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="8"
android:stateListAnimator="@null"
android:layout_marginStart="@dimen/dp_50"
android:textColor="@color/text_first_level_color"
android:textSize="@dimen/sp_28"
android:background="@drawable/keyboard_btn_bg_selector"/>
<Button
android:id="@+id/btn_of_9"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="9"
android:stateListAnimator="@null"
android:layout_marginStart="@dimen/dp_50"
android:textColor="@color/text_first_level_color"
android:textSize="@dimen/sp_28"
android:background="@drawable/keyboard_btn_bg_selector"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_marginTop="@dimen/dp_14"
android:gravity="center">
<Button
android:id="@+id/btn_of_del"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="删除"
android:stateListAnimator="@null"
android:textColor="@color/text_second_level_color"
android:textSize="@dimen/sp_24"
android:background="@drawable/keyboard_btn_bg_selector"/>
<Button
android:id="@+id/btn_of_0"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="0"
android:stateListAnimator="@null"
android:layout_marginStart="@dimen/dp_50"
android:textColor="@color/text_first_level_color"
android:textSize="@dimen/sp_28"
android:background="@drawable/keyboard_btn_bg_selector"/>
<Button
android:id="@+id/btn_of_sure"
android:layout_width="@dimen/dp_66"
android:layout_height="@dimen/dp_66"
android:text="确定"
android:stateListAnimator="@null"
android:layout_marginStart="@dimen/dp_50"
android:textColor="@color/keyboard_join_room_btn_txt_color"
android:textSize="@dimen/sp_24"
android:background="@drawable/keyboard_btn_bg_selector"/>
</LinearLayout>
</LinearLayout>
其次,采用继承LinearLayout的方式进行View扩展以达到自定义的目的。
可以看到,由于键盘上按钮功能比较单一,故避免采用重复方式进行控件初始化和相关操作,可以简化如下:
public class InputKeyboardView extends LinearLayout implements View.OnClickListener {
private static final int[] mBtnIdArray = {
R.id.btn_of_0, R.id.btn_of_1, R.id.btn_of_2, R.id.btn_of_3, R.id.btn_of_4, R.id.btn_of_5,
R.id.btn_of_6, R.id.btn_of_7, R.id.btn_of_8, R.id.btn_of_9, R.id.btn_of_del, R.id.btn_of_sure
};
private OnActionChangedListener mListener;
public void setOnActionChangedListener(OnActionChangedListener listener) {
this.mListener = listener;
}
public InputKeyboardView(Context context) {
super(context);
initView(context);
}
public InputKeyboardView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public InputKeyboardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
View view = inflate(context, R.layout.keyboard_layout, this);
for (int id : mBtnIdArray) {
view.findViewById(id).setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
if (v.getId() != NO_ID && mListener != null) {
for (int i = 0; i < mBtnIdArray.length; i++) {
if (v.getId() == mBtnIdArray[i]) {
if (mBtnIdArray[i] == R.id.btn_of_del) {
mListener.onDeleteClicked();
} else if (mBtnIdArray[i] == R.id.btn_of_sure) {
mListener.onSureClicked();
} else {
mListener.onAfterInput(i);
}
}
}
}
}
/**
* 输入文本变化的回调
*/
public interface OnActionChangedListener {
void onAfterInput(int num);
void onDeleteClicked();
void onSureClicked();
}
}
版权声明
本文为[建人尹口]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u012758497/article/details/103894386
边栏推荐
- 基于Transform的深度学习目标检测(DETR模型)
- P型MOS管开关电路及工作原理详解-KIA MOS管
- Easy to use screenshot software
- P8资料大放送
- Da14580ble light LED
- QT 事件过滤器实例
- js老生常谈之this,constructor ,prototype
- MOS管开关频率最高多少如何测算-KIA MOS管
- Sorting of remote sensing deep learning target detection data set
- MOS tube and MOS tube driving circuit case analysis - Kia MOS tube
猜你喜欢

Cloud native enthusiast weekly: looking for open source alternatives to netlify

浙政钉扫码登录

Depth first search (I): middle order traversal of binary tree (force buckle)

npm yarn 启动报错【已解决】

Project training - newspaper reading zombie

Transform based deep learning target detection (Detr model)

云原生爱好者周刊:寻找 Netlify 开源替代品

mySQL基础记录

支持AUTOSAR Classic以及Adaptive平台的DEXT诊断数据库

Various location codes applied in transformer model
随机推荐
SQL 语法
The VirtualBox virtual machine uses virtio net to run dpdk + VPP and cannot receive VLAN messages
QT event filter instance
L3-001 change (30 points) (pruning DFS)
Various location codes applied in transformer model
BI工具如何选型?这3个问题是关键
SQL create database
Memory management-
深度学习遥感场景分类数据集整理
Analysis of why power MOS tube is burned - Kia MOS tube
一文学会text-justify,orientation,combine文本属性
L3-007 ladder map (30 points) (condition DIJ)
Text justify, orientation, combine text attributes
L2-033 简单计算器 (25 分)
QT AxObject库的简单操作
Matplotlib tutorial 04 --- drawing commonly used graphics
SQL 数据库
How to select Bi tools? These three questions are the key
Kernel pwn 基础教程之 Heap Overflow
2022-04-21 mysql-innodb存储引擎核心处理