Android edittext 屬性inputtype詳解

原文鏈接:https://www.jianshu.com/p/4e238eb1deb2

 

上個星期看公司一個項目中靈活用到edittext 屬性inputtype。inputtype屬性不僅可以再xml裏面定義。還可以在activity設置的。項目應用場景是這樣。一個頁面需要很多的對話框輸入,包括需要字符型,數字型,或者密碼輸入。但是隻做了一個彈出對話框,然後不同調用這個對話框,用唯一碼識別那個按鍵調用彈出。那麼彈出來對話框(輸入類型)要靈活設置。就需要activity傳遞inputtype這個屬性值進行設置。因爲這個對話框都只調用了一個xml。這是一個場景吧。

一、先說說xml裏面的調用,這個都不陌生的說。

       <EditText
            android:id="@+id/edittext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number" />

這裏輸入是數字吧。

二、activity裏的調用。或者其他class。

EditText testEditText = (EditText) findViewById(R.id.edittext);
        int inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL;
        testEditText.setInputType(inputType);

這個inputType值是不會很好用啊。靈活吧!
那我貼圖看一下源碼中InputType屬性類吧。如下圖,

image

image

這上面貼就是inputype全部屬性了,這些多看看,都是英文單詞。

三、順便介紹一下xml的inputtype的值。 android:inputType=”none”
android:inputType=”text”
android:inputType=”textCapCharacters” 字母大寫
android:inputType=”textCapWords” 首字母大寫
android:inputType=”textCapSentences” 僅第一個字母大寫
android:inputType=”textAutoCorrect” 自動完成
android:inputType=”textAutoComplete” 自動完成
android:inputType=”textMultiLine” 多行輸入
android:inputType=”textImeMultiLine” 輸入法多行(如果支持)
android:inputType=”textNoSuggestions” 不提示
android:inputType=”textUri” 網址
android:inputType=”textEmailAddress” 電子郵件地址
android:inputType=”textEmailSubject” 郵件主題
android:inputType=”textShortMessage” 短訊
android:inputType=”textLongMessage” 長信息
android:inputType=”textPersonName” 人名
android:inputType=”textPostalAddress” 地址
android:inputType=”textPassword” 密碼
android:inputType=”textVisiblePassword” 可見密碼
android:inputType=”textWebEditText” 作爲網頁表單的文本
android:inputType=”textFilter” 文本篩選過濾
android:inputType=”textPhonetic” 拼音輸入
//數值類型
android:inputType=”number” 數字
android:inputType=”numberSigned” 帶符號數字格式
android:inputType=”numberDecimal” 帶小數點的浮點格式
android:inputType=”phone” 撥號鍵盤
android:inputType=”datetime” 時間日期
android:inputType=”date” 日期鍵盤
android:inputType=”time” 時間鍵盤

四、介紹edittext其他常用屬性,網絡上收集的。

android:layout_gravity="center_vertical" 設置控件顯示的位置:默認top,這裏居中顯示,還有bottom android:hint="請輸入數字!"設置顯示在空間上的提示信息

android:numeric="integer" 設置只能輸入整數,如果是小數則是:decimal
android:singleLine="true" 設置單行輸入,一旦設置爲true,則文字不會自動換行。
android:password="true" 設置只能輸入密碼
android:textColor = "#ff8c00" 字體顏色
android:textStyle="bold" 字體,bold, italic, bolditalic

android:textSize="20dip" 大小
android:capitalize = "characters" 以大寫字母寫
android:textAlign="center" EditText沒有這個屬性,但TextView有 android:textColorHighlight="#cccccc" 被選中文字的底色,默認爲藍色
android:textColorHint="#ffff00" 設置提示信息文字的顏色,默認爲灰色

android:textScaleX="1.5" 控制字與字之間的間距
android:typeface="monospace" 字型,normal, sans, serif, monospace

android:background="@null" 空間背景,這裏沒有,指透明
android:layout_weight="1" 權重,控制控件之間的地位,在控制控件顯示的大小時蠻有用的。
android:textAppearance="?android:attr/textAppearanceLargeInverse" 文字外觀,這裏引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,否則使用默認的外觀。不知道這樣理解對不對?

通過EditText的layout xml文件中的相關屬性來實現:
1. 密碼框屬性 android:password="true" 這條可以讓EditText顯示的內容自動爲星號,輸入時內容會在1秒內變成*字樣。
2. 純數字 android:numeric="true" 這條可以讓輸入法自動變爲數字輸入鍵盤,同時僅允許0-9的數字輸入
3. 僅允許 android:capitalize="cwj1987" 這樣僅允許接受輸入cwj1987,一般用於密碼驗證 下面是一些擴展的風格屬性
android:editable="false" 設置EditText不可編輯
android:singleLine="true" 強制輸入的內容在單行
android:ellipsize="end" 自動隱藏尾部溢出數據,一般用於文字內容過長一行無法全部顯示時

雖然這些都是很簡單,但也很常用。細節需要注意,才能做到極致。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章