zoukankan      html  css  js  c++  java
  • Android编程 EditView 中如何设置最多可以输入的字符数量 属性 android:ems 与 android:maxLength 的区别

    最近有一个新的感悟,那就是工作的时候千万不要遇到那种特要人无语的领导,很不幸我现在就遇到了这样的一个领导,说是要给领导认识的一个熟人家的孩子写本科毕业设计预算把我给派过去给本科生写毕业设计,这事情的确要人十分的无语,为了完成领导交代的任务还是要好好去弄,于是乎我现在变搞起了Android编程,这个以前未做过的方向。

    现在遇到了这样的一个界面,在Android编程中这叫做Activity, 如下图:

    其中,需要弄一个EditText 组件, 也就是做那个输入姓名的那个东西,为了限制输入的字符需要对EditText组件进行设置,在网上搜到了   android:ems  属性的设置。

    但是很奇怪的事情是 虽然设置了  android:ems 属性,但是输入的字符数量并没有任何限制。

    有文章指出:

    https://blog.csdn.net/caroline_wendy/article/details/41684255

    xml中android:ems属性,作为EditText默认生成的属性,其含义是需要编辑的字符串长度
    设置为10时,最多编辑10个em,一个em单位是两个inch,但是随着自动调整,在Android中em代表‘M’的数量
    但是EditText的属性,只有在android:layout_width=“wrap_content”时,才会显示;
    如果是android:layout_width=“match_parent”时,则不会有变化。

    但是,在我的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>
  • 相关阅读:
    C# 依据鼠标坐标取网页内成员坐标.ie
    C# WebBrowser获取指定字符串的坐标
    C#获取网页中某个元素的位置,并模拟点击
    qq空间认证教程:借助企鹅媒体平台认证QQ公众空间
    QQ空间认证之数据篇
    QQ空间运营 怎么做一个QQ人气号?
    QQ空间|qq人气号怎么赚钱?
    QQ好友的价值玩法 及如何搞到几万好友?
    新媒体运营之如何月涨十万粉
    社群经济:如何利用社群做营销?
  • 原文地址:https://www.cnblogs.com/devilmaycry812839668/p/8675586.html
Copyright © 2011-2022 走看看