你從未見過的EditText屬性詳解

前言

Hi,小夥伴們,Layout學會了,ButtonTextView學會了,ImageView也學會了,是不是感覺總是學習這些單一的東西稍微有點枯燥了呢?那麼學習了這篇文章之後,開始盡情發揮你們的想象力開始搞事情吧~

這一篇我們講解EditText的使用,EditText是一個輸入框,在Android開發中是常用的控件。也是獲取用戶數據的一種方式,EditTextTextView的子類,它繼承了TextView的所有屬性。話不多說,讓我們趕緊開始學習吧~

簡介

EditText的結構

public class EditText
extends TextView

android.view.View
↳android.widget.TextView
↳ android.widget.EditText

常用屬性

1.默認提示文本的兩個屬性如下

android:hint="默認提示文本" <!--和微信需要登錄時一樣,在文本框中提示的扣扣號,郵箱-->
android:textColorHint="#95A1AA" <!--提示的顏色-->

2.獲得焦點後全選組件內所有文本內容

android:selectAllOnFocus="true"

3.限制EditText輸入類型

<EditText   
        android:layout_width="match_parent"   
        android:layout_height="wrap_content"   
        android:inputType="phone" />

4.文本類型,多爲大寫、小寫和數字符號,可選參數如下

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" <!--時間-->

5.設置最小行,最多行,單行

android:minLines="3" <!--設置最小行的行數-->
android:maxLines="10" <!--最大的行數,當輸入內容超過maxlines,文字會自動向上滾動-->
android:singleLine="true" <!--限制EditText只允許單行輸入,而且不會滾動-->

6.設置文字間隔,設置英文字母大寫類型

android:textScaleX="1.5" <!--設置字與字的水平間隔-->
android:textScaleY="1.5" <!--設置字與字的垂直間隔-->
android:capitalize="none" <!--sentences:僅第一個字母大寫;words:每一個單詞首字母大小,用空格區分單詞;characters:每一個英文字母都大寫-->

7.控制EditText四周的間隔距離與內部文字與邊框間的距離

android:paddingTop="5dp" <!--使用margin相關屬性增加組件相對其他控件的距離,比如android:marginTop="5dp";使用android:paddingTop="5dp"增加組件內文字和組件邊框的距離-->

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

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",文本將居中顯示-->
代碼實例講解

通過上面瞭解EditText輸入框的常用屬性之後,結合上幾講TextView、Button我們通過代碼示例來講解實現一個簡單的登錄頁面效果

示例xml佈局代碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:text="登錄"
        android:textColor="#008577"
        android:textSize="30sp" />
            
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="180dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical">
        
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="帳號"
                    android:textColor="#000000"
                    android:textSize="18sp" />

                <EditText
                    android:id="@+id/et_mobile"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="20dp"
                    android:layout_weight="1"
                    android:background="#00000000"
                    android:gravity="center"
                    android:hint="請輸入手機號"
                    android:inputType="phone"
                    android:maxLength="15"
                    android:singleLine="true"
                    android:textColorHint="#008577"
                    android:textSize="18sp" />

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="10dp"
                android:background="#FF4413" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="密碼"
                    android:textColor="#000000"
                    android:textSize="18sp" />

                <EditText
                    android:id="@+id/et_password"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="40dp"
                    android:layout_weight="1"
                    android:background="#00000000"
                    android:gravity="center"
                    android:hint="請輸入密碼"
                    android:inputType="textPassword"
                    android:maxLength="30"
                    android:singleLine="true"
                    android:textColorHint="#008577"
                    android:textSize="18sp" />
                        
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="10dp"
                android:background="#FF4413" />

        </LinearLayout>

        <Button
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="55dp"
            android:background="#81D841"
            android:clickable="false"
            android:text="登錄"
            android:textColor="#ffffff"
            android:textSize="18sp"
            android:textStyle="bold" />

    </LinearLayout>

</RelativeLayout>
xml佈局預覽效果圖如下

常用方法

1.設置焦點,光標的位置

EditText et = (EditText) findViewById(R.id.et_mobile); 
et.setFocusable(true);
et.requestFocus();
et.setFocusableInTouchMode(true);

et.clearFocus();//失去焦點 
et.requestFocus();//獲取焦點

2.設置默認輸入法

et.setInputType(EditorInfo.TYPE_CLASS_TEXT); //中文鍵盤
et.setInputType(EditorInfo.TYPE_TEXT_VARIATION_URI); //英文鍵盤
et.setInputType(InputType.TYPE_CLASS_NUMBER); //數字鍵盤

3.強制顯示隱藏軟鍵盤

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);  
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //隱藏軟鍵盤
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); //顯示軟鍵盤

4.始終不彈出軟鍵盤

//在XML文件中,Edittext父佈局上進行如下設置
android:focusable="true"   
android:focusableInTouchMode="true"
    
//在Java代碼中,添加下面屬性
et.setInputType(InputType.TYPE_NULL);

5.顯示隱藏密碼

//在XML文件中設置
android:password="true" <!-- 以”.”形式顯示密碼文本-->
android:inputType="textPassword" <!--不可見密碼-->
android:inputType="textVisiblePassword" <!--可見密碼-->

//在Java代碼中設置
et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隱藏密碼
et.setTransformationMethod(PasswordTransformationMethod.getInstance());//顯示密碼

6.獲取文本最大長度,由於EditText沒有提供獲取最大長度方法,需要用到反射

public static int getMaxLength(EditText et) {
         int length = 0;
         try {
             InputFilter[] inputFilters = et.getFilters();
             for (InputFilter filter : inputFilters) {
                 Class<?> c = filter.getClass();
                 if (c.getName().equals("android.text.InputFilter$LengthFilter")) {
                     Field[] f = c.getDeclaredFields();
                     for (Field field : f) {
                         if (field.getName().equals("mMax")) {
                             field.setAccessible(true);
                             length = (Integer) field.get(filter);
                         }
                     }
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return length;
     }

7.文本監聽事件,監控當前輸入的文本長度,需要實現TextWatcher接口

private class TextChangedTextWatcher implements TextWatcher {
    
         private EditText mView;
         private int mMaxLength;
         private CharSequence mMobile;
 
         public TextChangedTextWatcher(EditText v) {
             super();
             mView = v;
             mMaxLength = getMaxLength(v);
         }
 
         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
             //文本改變前
         }
 
         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) {
             //文本改變時
             mMobile = s;
         }
 
         @Override
         public void afterTextChanged(Editable s) {
             //文本改變後,一般使用此方法
             if (mMobile == null || mMobile.length() == 0)
                 return;
             if (mMobile.length() == 11 && mMaxLength == 11) {
                 
             }
         }
     }

結語

看完以上內容,是不是也迫不及待想實現一個登錄或者註冊的頁面了!!那還等什麼,趁着記憶力深刻,讓我們一起開始吧,也請各位多多點贊留言哦~ 如果小夥伴想學習更多知識或者快速學習進階,可以加入我們的微信羣一起探討~ 在公衆號中回覆微信羣,就可以加入其中,也可以在公衆號中回覆視頻,裏面有一些初學者視頻哦~

PS:如果還有未看懂的小夥伴,歡迎加入我們的QQ技術交流羣:892271582,裏面有各種大神回答小夥伴們遇到的問題,我們的微信羣馬上也將要和大家見面啦,屆時希望大家踊躍加入其中~~

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