最近有一個新的感悟,那就是工作的時候千萬不要遇到那種特要人無語的領導,很不幸我現在就遇到了這樣的一個領導,說是要給領導認識的一個熟人家的孩子寫本科畢業設計預算把我給派過去給本科生寫畢業設計,這事情的確要人十分的無語,為了完成領導交代的任務還是要好好去弄,於是乎我現在變搞起了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>