Android Studio 的原生輸入框控件 EditText 屬性配置詳解

Android Studio 的原生輸入框控件 EditText 屬性配置詳解

文本設置

android:hint="默認文本設置"
android:textColorHint="#95A1AA" // 默認文本顏色
android:textColorHighlight=“#666666” // 文字選中顏色
android:selectAllOnFocus="true" // 獲取焦點後是否全選文本內容
android:textScaleX="1.5" // 設置字與字的水平間隔
android:textScaleY="1.5" // 設置字與字的垂直間隔
android:capitalize 默認none,提供了三個可選值:
	sentences:僅第一個字母大寫
	words:每一個單詞首字母大小,用空格區分單詞
	characters:每一個英文字母都大寫
android:paddingTop="5dp" // 文字與組件邊框的距離

文本類型

android:inputType="none" <!--無格式-->
android:inputType="phone" <!--撥號鍵盤-->
android:inputType="text" <!--文本格式-->
android:inputType="textAutoComplete" <!--自動完成-->
android:inputType="textAutoCorrect" <!--糾正單詞的拼寫錯誤-->
android:inputType="textCapCharacters" <!--所有字符大寫-->
android:inputType="textCapSentences" <!--僅第一個字母大寫-->
android:inputType="textCapWords" <!--單詞首字母大寫-->
android:inputType="textEmailAddress" <!--電子郵件地址格式-->
android:inputType="textEmailSubject" <!--郵件主題格式-->
android:inputType="textFilter" <!--文本篩選格式-->
android:inputType="textImeMultiLine" <!--輸入法多行-->
android:inputType="textLongMessage" <!--長消息格式-->
android:inputType="textMultiLine" <!--多行輸入-->
android:inputType="textNoSuggestions" <!--不提示-->
android:inputType="textPassword" <!--密碼格式-->
android:inputType="textPersonName" <!--人名格式-->
android:inputType="textPhonetic" <!--拼音輸入格式-->
android:inputType="textPostalAddress" <!--郵政格式-->
android:inputType="textShortMessage" <!--短消息格式-->
android:inputType="textUri" <!--URI格式-->
android:inputType="textVisiblePassword" <!--密碼可見格式-->
android:inputType="textWebEditText" <!--作爲網頁表單的文本格式-->
android:inputType="textWebEmailAddress" <!--作爲網頁表單的電子郵件地址格式-->
android:inputType="textWebPassword" <!--作爲網頁表單的密碼格式-->

數值類型

android:inputType="numberPassword" <!--數字密碼格式-->
android:inputType="numberSigned" <!--有符號數字格式-->
android:inputType="number" <!--數字格式-->
android:inputType="numberDecimal" <!--可以帶小數點的浮點格式-->
android:inputType="phone" <!--撥號鍵盤-->
android:inputType="datetime" <!--日期-->
android:inputType="date" <!--日期-->
android:inputType="time" <!--時間-->

行數設置

android:minLines="3" // 最小行數
android:maxLines="3" // 最大行數
android:singleLine="true" // 設置只允許單行輸入,而且不會滾動

改變輸入法中回車按鈕的顯示內容

android:imeOptions="actionNone"
<!--imeOptions有下面一些常用值-->
<!--actionUnspecified未指定,對應常量EditorInfo.IME_ACTION_UNSPECIFIED;-->
<!--actionNone 沒有動作,對應常量EditorInfo.IME_ACTION_NONE;-->
<!--actionGo執行 “開始” ,對應常量EditorInfo.IME_ACTION_GO;-->
<!--actionSearch 執行 “搜索”,對應常量EditorInfo.IME_ACTION_SEARCH;-->
<!--actionSend執行 “發送”,對應常量EditorInfo.IME_ACTION_SEND;-->
<!--actionNext 執行 “下一個”,對應常量EditorInfo.IME_ACTION_NEXT;-->
<!--actionPrevious 執行 “上一個”,對應常量IME_ACTION_PREVIOUS;-->
<!--actionDone 執行 “完成”,對應常量EditorInfo.IME_ACTION_DONE-->

其他屬性設置

android:numeric="integer" <!--設置僅僅能輸入整數,假設是小數則是:decimal-->
android:password="true" <!--設置僅僅能輸入密碼-->
android:textColor="#ff8c00" <!--字體顏色-->
android:textStyle="bold" <!--字體 bold, italic, bolditalic-->
android:textSize="20dp" <!--設置輸入文本內容字體大小-->
android:textAlign="center" <!--EditText沒有這個屬性,但TextView有,居中-->
android:typeface="monospace" <!--字型,normal, sans, serif, monospace (標準、無襯線字體、襯線字體、等寬字體)-->
android:background="@null" <!--背景,這裏設置null,意思爲透明-->
android:layout_weight="1" <!--權重,控制控件之間的地位,在控制控件顯示的大小時蠻實用的-->
android:cursorVisible="true" <!--設定光標爲顯示/隱藏,默認顯示-->
android:digits="1234567890" <!--設置允許輸入哪些字符,如“1234567890.+-*/% ()”-->
android:drawableRight="@drawable/xxx" <!--在EditText的右邊輸出一個drawable-->
android:drawableTop="@drawable/xxx" <!--在EditText的正上方輸出一個drawable-->
android:drawableBottom="@drawable/xxx" <!--在EditText的下方輸出一個drawable-->
android:drawableLeft="@drawable/xxx" <!--在EditText的左邊輸出一個drawable-->
android:drawablePadding <!--設置text與drawable(圖片)的間隔,與drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可設置爲負數,單獨使用沒有效果-->
android:editable="true" <!--設置是否可編輯,默認可以編輯-->
android:ellipsize="start" <!--設置當文字過長時,該控件該怎樣顯示,例如設置以下值:"start"省略號顯示在開頭,"end"省略號顯示在結尾,"middle"省略號顯示在中間,"marquee"以跑馬燈的方式顯示(動畫橫向移動)-->
android:gravity="center" <!--設置文本位置,如設置成"center",文本將居中顯示-->

修改光標的顏色和樣式

1、在 drawable 文件夾下新建一個 edit_cursor_color.xml 文件,寫入 shape 標籤,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="1dp" />
    <solid android:color="#000000" />
</shape>

2、在佈局文件中引用 EditText 控件時設置:

android:textCursorDrawable="@drawable/edit_cursor_color"

爲 EditText 配置邊框線

1、在 drawable 中寫出一個邊框線:textview_border.xml

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape>
        <stroke android:width="0.5dp" android:color="#e1e1e1" /><!--邊框顏色-->
        <solid android:color="#f8f8f8" /><!--填充色-->
        <corners android:radius="4dp" />
    </shape>
  </item>
</layer-list>

2、在需要配置的 EditText 加入 background 屬性就好了

設置 EditText 默認不聚焦

在其父佈局中添加:

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