当前位置:网站首页>UGUI系列-InputField限制输入个数以及限制输入格式
UGUI系列-InputField限制输入个数以及限制输入格式
2022-08-07 09:56:00 【恬静的小魔龙】
一、前言
UGUI InputField 组件是一个用来管理输入的组件 我们通常用来输入用户的账号,密码,或者聊天时输入文字,等等输入逻辑…
在使用中,我们常常要对输入的字符串进行限制,最常见的限制有个数和格式
二、限制输入个数
设置Input Field组件的 Character Limit 属性就可以限制输入个数:
如下图所示:
三、限制输入类型
这个我们就需要研究一下Input Field组件的 Content Type 属性了
这个属性的功能是定义输入内容接受/限制的字符类型
一共有以下几种类型:
类型 | 功能 |
|---|---|
Standard | 标准,可以输入任意字符 |
Auto corrected | 自动更正,可以自动更正用户输入,并建议输入内容 |
Integer Number | 整数,只允许输入整数 |
Decimal Number | 小数,允许输入数字和小数点后一位 |
Alphanumeric | 字母数字,允许字母和数字。无法输入符号 |
Name | 名称(支持中文)自动将每个单词的首字母大写。 |
Email Address | 电子邮件地址,允许您输入最多包含一个@符号的字母数字字符串 |
Password | 密码:用 * 表示输入的字符,从而隐藏输入内容。允许输入任意字符 |
Pin | 密码:用 * 号表示输入的字符,从而隐藏输入内容。仅允许输入整数 |
Custom | 自定义:允许自定义行类型、输入类型、默认键盘类型、字符验证 |
这几个就没啥好说的,
主要讲一下自定义:
Line Type:行类型,允许输入单行或者多行,或者Enter 键来新建行,继续输入
Input Type:输入类型,有三个选项,任何字符、自动修正、密码类型
Keyboard Type:它可以在用户选中输入框时,调出不同类型的键盘
名称 | 功能 |
|---|---|
Default | 目标平台的默认键盘 |
ASCLL Capable | 带标准ASCII键的键盘。 |
Numbers And Punctuation | 键盘与数字和标点符号键 |
URL | 键盘与URL输入键 |
Number Pad | 键盘与标准数字键 |
Phone Pad | 键盘与适合键入电话号码的布局 |
NamePhone Pad | 键盘与字母数字键 |
Email Address | 带有适合键入电子邮件地址的其他键的键盘 |
Nintendo Network Account | 带有网络账号键的键盘 |
Social | 键盘与常用于社交媒体上的符号键,如Twitter |
Search | 键盘上带有“.” 空格键旁边的键,适合键入搜索词 |
Character Validatior:字符验证类型,有整数、小数、字母数字、名字、Email等
按需设置咯
如果上面还不满足你的设置,想要更加严格的限制,那么请往下看
四、代码限制输入字符
比如说用正则表达式来判断输入的字符串是否符合格式,符合格式就不管,不符合格式就把赋值输入的字符串减一位的字符串,也就相当于删除掉最后一位
关于正则表达式的应用可以看我另一篇文章:文章链接
4-1、限制输入的字符串0-9 a-f A-F
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
public class Input_Test : MonoBehaviour
{
InputField m_InputField;
private void Awake()
{
m_InputField = GetComponent<InputField>();
m_InputField.onValueChanged.AddListener(OnInputFieldValueChang);
}
private void OnInputFieldValueChang(string inputInfo)
{
Regex reg = new Regex("^[A-Fa-f0-9]+$");
if (reg.IsMatch(inputInfo))
{
m_InputField.text = inputInfo;
}
else
{
if (m_InputField.text == "")
{
m_InputField.text = "";
}
else
{
m_InputField.text = inputInfo.Substring(0, inputInfo.Length - 1);
}
}
}
}边栏推荐
- 100天精通Andriod逆向——第1天:ADB原理及其常用命令
- 关于常用构建工具的一些总结
- 10 things he learned after teaching most of his life at MIT
- 颜值爆表!Redis官方可视化工具来啦,功能真心强大!
- The principle and source code of redis - basic introduction
- What do you think of Douyin's China Video Partner Program?
- 牛客多校-Eezie and Pie-(倍增+树上差分)
- GPT - 3 combination DALL e., 60 seconds to get the game set and original animation!After the net friend to see: want to play this game
- 【位带操作对寄存器赋值】基于ADuCM4050的GPIO复用模式初始化
- 你如何看待抖音的中视频伙伴计划的?
猜你喜欢

During the meeting, I was scolded by the leader for more than 10 minutes. The scolding made me almost cry, and my voice was shaking.

MySQL advanced (twenty-eight) GRANT REVOKE usage details

数据可视化(三)

Material Design

ctf (easy_md5)

GPT - 3 combination DALL e., 60 seconds to get the game set and original animation!After the net friend to see: want to play this game

LeetCode #94.二叉树的中序遍历

解决Redis、MySQL缓存双写不一致问题

2022牛客多校联赛第六场 题解

php怎么判断目录下有几个文件
随机推荐
解决Redis、MySQL缓存双写不一致问题
技术总监需要具备哪些能力?优先级?
如何仿造websocket请求?
大厂外包,值得拥有吗?
The 900-page mathematical paper proves that the rotating black hole will not explode, Chengtong Yau: The first major breakthrough in general relativity in more than 30 years...
mysql进阶(二十八)GRANT REVOKE用法详解
Flask框架——基于类的视图
process scheduling
LeetCode #94.二叉树的中序遍历
【.NET6+Modbus】Modbus TCP协议解析、仿真环境以及基于.NET实现基础通信
1324_FreeRTOS队列创建函数实现分析
npm 发布问题
Three squirrel asked staff to keep the squirrel's smile, according to the customer for the "master"
Redis只能做缓存?太out了!
查询文件和目录的磁盘使用
Product "free gift" + premium investment, Sanofi fired the first shot of bottom-hunting China's biotech assets
xlwings模块(数据保存为xlsx文件)
[C language] Tower of Hanoi
The principle and source code of redis-redis memory elimination strategy & LRU source code analysis & LFU source code analysis
12 个适合做外包项目的开源后台管理系统