Android edittext 屬性inputtype詳解

來源:互聯網
上載者:User

標籤:android   layout   

上個星期看公司一個項目中靈活用到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屬性類吧。如,

這上面貼就是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” IME多行(如果支援)
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" 這條可以讓IME自動變為數字輸入鍵盤,同時僅允許0-9的數字輸入
3. 僅允許 android:capitalize="cwj1987" 這樣僅允許接受輸入cwj1987,一般用於密碼驗證 下面是一些擴充的風格屬性
android:editable="false" 設定EditText不可編輯
android:singleLine="true" 強制輸入的內容在單行
android:ellipsize="end" 自動隱藏尾部溢出資料,一般用於文字內容過長一行無法全部顯示時

 

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

 

 

Android edittext 屬性inputtype詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.