Android编程 EditView 中如何设置最多可以输入的字符数量 属性 android:ems 与 android:maxLength 的区别
最近有一个新的感悟,那就是工作的时候千万不要遇到那种特要人无语的领导,很不幸我现在就遇到了这样的一个领导,说是要给领导认识的一个熟人家的孩子写本科毕业设计预算把我给派过去给本科生写毕业设计,这事情的确要人十分的无语,为了完成领导交代的任务还是要好好去弄,于是乎我现在变搞起了Android编程,这个以前未做过的方向。
现在遇到了这样的一个界面,在Android编程中这叫做Activity, 如下图:
其中,需要弄一个EditText 组件, 也就是做那个输入姓名的那个东西,为了限制输入的字符需要对EditText组件进行设置,在网上搜到了 android:ems 属性的设置。
但是很奇怪的事情是 虽然设置了 android:ems 属性,但是输入的字符数量并没有任何限制。
有文章指出:
https://blog.csdn.net/caroline_wendy/article/details/41684255
但是,在我的APP中我发现这么一个事情,那就是不论 android:layout_width 设置为wrap_content 还是 match_parent 都没有起到限制输入字符数量的作用。
以下文章给出其他解释:
https://blog.csdn.net/qq_33618323/article/details/66477490
那就是 android:ems 是限制显示的数量。
但是在我的APP中这一点也没有被证实,可能是我的APP上某些设置还是存在一定的问题的。
最后,https://www.cnblogs.com/liaojie970/p/5834106.html
给出
android:maxLength
的用法,该属性设置可以被用来设置EditText中的最大可输入的字符串数量。
也就是说,要限制 输入的字符数量 我们应该使用属性 Android:maxLength 来进行设置。
android:ems 和 android:layout_width="wrap_content” 搭配使用可以限制 显示字符的数量,当然这还和Layout的设置和其他的设置有关。如果采用RelativeLayout布局下的TextView组件中显示的字符串有可能不受限制。
不过最终也没有太搞明白 Android:ems的具体含义是什么,不过网上有网友做过这方面的尝试,以下给出链接,个人认为下面这个资料还是不错的,至少从多个方面尝试过,虽然最后还是不太好解释通。
https://blog.csdn.net/beiminglei/article/details/9317997
不过,上文中给出了一个所能找到的一个不错的解释,如下:
https://blog.csdn.net/JavaLive09/article/details/38661773
em是一个印刷排版的单位,表示字宽的单位。 em字面意思为:equal M (和M字符一致的宽度为一个单位)简称em。
ems是em的复数表达。
最终的代码实现:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myapplication3.MainActivity"> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:text="姓:"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:textSize="30sp" />
<EditText
android:maxLength="10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText1"
android:layout_weight="1"
android:textSize="30sp"
android:hint="(请输入姓)"
android:maxLines="1"
android:textAlignment="center" />
</LinearLayout> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="名:"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:textSize="30sp" />
<EditText
android:maxLength="10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText2"
android:layout_weight="1"
android:textSize="30sp"
android:hint="(请输入名)"
android:maxLines="1"
android:textAlignment="center" />
</LinearLayout> </LinearLayout>
Android编程 EditView 中如何设置最多可以输入的字符数量 属性 android:ems 与 android:maxLength 的区别的更多相关文章
- xamarin android 在代码中如何设置文本颜色
xamarin android 在代码中如何设置文本颜色 TextView v = FindViewById<TextView>(Android.Resource.Id.Message); ...
- TextView设置最多显示指定个字符,超过部分显示...(省略号)
今天在公司遇到一个需求:TextView设置最多显示8个字符,超过部分显示...(省略号),网上找了很多资料,有人说分别设置TextView的android:signature="true& ...
- Android编程学习过程中遇到的错误以及解决办法
Android Studio遇到的问题 Android Studio一直提示右下角那个错误,Failed to open zip file.我是根据这位大佬的博客https://www.cnblogs ...
- 001-Mono for android在vs2012中发布设置
详细讲解 :http://www.cr173.com/html/20099_1.html 这里是快速设置: 1,把Debug换成Release 这里是必须改的 2,点击工具选项 3, ...
- .NET混合开发解决方案8 WinForm程序中通过设置固定版本运行时的BrowserExecutableFolder属性集成WebView2控件
系列目录 [已更新最新开发文章,点击查看详细] 在我的博客<.NET混合开发解决方案7 WinForm程序中通过NuGet管理器引用集成WebView2控件>中介绍了WinForm ...
- Android 手机卫士13--进程设置
1.显示隐藏系统进程 修改ProcessManagerActivity的Adapter ..... @Override public int getCount() { if(SpUtil.getBoo ...
- 在android的spinner中,实现取VALUE值和TEXT值。 ZT
在android的spinner中,实现取VALUE值和TEXT值. 为了实现在android的 spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器, ...
- Android编程中的实用快捷键
作为一个优秀的程序员,不但要能开发出漂亮的软件,也要能熟练掌握编程的技巧,包括IDE的快捷键使用.比如linux 下的VI编辑器,对于不熟练快捷键的人来说就是一个噩梦,但一旦你熟练了VI的快捷键,VI ...
- Android编程中的5种数据存储方式
Android编程中的5种数据存储方式 作者:牛奶.不加糖 字体:[增加 减小] 类型:转载 时间:2015-12-03我要评论 这篇文章主要介绍了Android编程中的5种数据存储方式,结合实例形式 ...
随机推荐
- mysql中一张(居民)表按年龄段查询数据
知识点: 用mysql,按年龄段查询一张居民的数据(各年龄段居民的个数) 1.如:查询resident(居民表),按照各年龄段,统计人数 2.mysql语句如下: select ageproporti ...
- Python学习札记(二) python3.5安装 + (假装是)第一个Python程序
参考: Mac OS 安装 Python3.5 廖雪峰Python教程:安装Python 笔记: 安装: 1.官方网站下载安装包:安装程序 2.执行安装程序完成Python3.5包的安装. 3.安装P ...
- "ImportError: cannot import name OVSLegacyKernelSwitch"
My VirtualMachine OS is Ubuntu14.04, Linux. Recently I want to install the mininet in my OS, and I u ...
- CentOs64位编译安装hadoop-2.6.0
官方提供的hadoop-2.x版本貌似都是32位的,在64位机子下使用可能会报错,最好使用官方提供的源码进行本地编译,编译成适合本地硬件环境的64位软件包. Hadoop是使用Java语言开发的,但是 ...
- vc libcurl 模拟上传文件
http://www.cnblogs.com/killbit/p/5393301.html 附上这篇文章,因为当时就已经想到了模拟上传,但是因为时间关系,所以就直接用PHP写了.现在改进一下,用VC+ ...
- 外国人专门写了一篇文章,来分析为什么go在中国如此火
外国人专门写了一篇文章,来分析为什么go在中国如此火: <Why is Golang popular in China?> http://herman.asia/why-is-go-pop ...
- ReverseLinkedList,ReverseLinkedList2,链表反转问题
ReverseLinkedList: public class ReverseLinkedList { public ListNode reverseList(ListNode head) { if( ...
- 清理网页中的HTML
public string ClearHtml(string text)//过滤html,js,css代码 { text = text.Trim(); if (string.IsNullOrEmpty ...
- Gruntjs提高生产力(一)
gruntjs是一个基于nodejs的自动化工具,只要熟悉nodejs或者又一定js经验就可以熟练应用. 1. 安装 a. 保证已安装了nodejs,并带有npm b.安装客户端命令行工具,grunt ...
- struts-2.3.24.1中的jar的详解
Struts2.3.24.1核心Jar包详解 antlr-2.7.2.jar 语言转换工具,它是接受词文法语言描述,并能产生识别这些语言的语句的程序的一种工具 a ...