天天看點

EditText(文本框)

EditText:

常用比較重點的屬性:

android:phoneNumber=”true”該文本框是一個電話框

android:digits=”abc”指輸入内容限制為指定内容

android:drawableLeft=”@drawable/icon”設定文本左邊繪制指定圖像

android:drawablePadding設定文本框内文本與圖形間的間距

android:selectAllOnFocus=”true”獲得焦點時自動選中所有文本

android:elipsize=”middle”設定文本超過TextView長度時如何處理文本

android:cursorVisible=”true”設定文本框光标是否可見

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:type 字型,normal, sans, serif, monospace

android:background="@null" 空間背景,指透明

android:layout_weight="1" 權重,控制控件之間的地位,可用于在控制控件顯示的大小。

EditText文本框簡單代碼

<EditText

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginLeft="20dp"

    android:layout_marginRight="20dp"

    android:background="@null"

    android:inputType="number"

    android:maxLength="11"

    android:hint="請輸入手機号"

    android:drawablePadding="10dp"

    android:padding="10dp"

    android:layout_marginTop="20dp"/>



<EditText

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginLeft="20dp"

    android:layout_marginRight="20dp"

    android:layout_marginTop="10dp"

    android:background="@null"

    android:inputType="textPassword"

    android:maxLength="16"

    android:padding="10dp"

    android:hint="請輸入密碼"/>



<Button

    android:id="@+id/btn_login_submit"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_margin="10dp"

    android:background="@color/colorPrimary"

    android:textColor="@color/colorWhite"

    android:textSize="22sp"

    android:text="登 錄"/>      
EditText(文本框)

上圖控件文本處于一個置頂的位置

權重占位,使文本居中      
權重:      
<TextView 

    android:layout_width="wrap_content"

    android:layout_height="0dp"

    android:layout_weight="1"/>      

權重占位,android:layout_weight="1"

如果上下使用android:layout_weight="1"則是達到居中的效果

如果上android:layout_weight="1";下是android:layout_weight="2"

則文本的位置會往上頂1占據了3分之2的部分