Android Studio 之 EditText


 

EditText 簡介

•簡介

  EditText是一個非常重要的組件,可以說它是用戶和Android應用進行數據傳輸窗戶;

  有了它就等於有了一扇和Android應用傳輸的門,通過它用戶可以把數據傳給Android應用,然后得到我們想要的數據。

  EditText是TextView的子類,所以TextView的方法和特性同樣存在於EditText中

 

 


EditText常用屬性

•maxLength

  有時候我們有一些特屬的需要,要求只能在EditText中輸入特定個數的字符,比如身份證號、手機號碼等。

  這時候就可以通過 android:maxLength 屬性來設置最大輸入字符個數;

  比如 android:maxLength=“4” 就表示最多能輸入 4 個字符,再多了就輸入不進去了。

•Hint

  有時候我們需要說明你定義的這個EditText是做什么用的,比如讓輸入“用戶名”,或者輸入“電話號碼”等;

  但是你又不想在EditText前面加一個TextView來說明這是輸入“用戶名”的,因為這會使用一個TextView,那么怎么辦呢?

  EditText為我們提供了android:hint來設置當EditText內容為空時顯示的文本;

  這個文本只在EditText為空時顯示,你輸入字符的時候就消失了,不影響你的EditText的文本。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入用戶名"/>

</LinearLayout>

運行效果:

  

  我們還可以通過 android:textColorHint 屬性設置提示文本的顏色;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入用戶名"
        android:textColorHint="@color/black"
        />

</LinearLayout>

•enabled

  設置 android:enabled="false" 可以實現不可編輯,可以獲得焦點。

  這時候我們看到EditText和一個TextView差不多;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入用戶名"
        android:textColorHint="@color/black"
        android:enabled="false"
        />

</LinearLayout>

運行效果:

  

•password

  密碼輸入也是Android應用常用的功能,通過配置EditText的android:password="true"就可以實現這一密碼輸入功能;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入密碼"
        android:textColorHint="@color/black"
        android:password="true"
        />

</LinearLayout>

運行效果:

  

  設置之后,我們輸入的字符就會被 “.” 這樣的掩碼所代替。

•phoneNumber

  手機中發短信打電話是必不可少的,所以用於專門輸入電話號碼的文本框也是大有用途;

  有了他我們對是否是電話號碼的校驗就容易的多了(因為字符是正確的,只要校驗格式 );

  通過設置 android:phoneNumber="true" 就可以把EditText變成只接受電話號碼輸入的文本框;

•inputType

  前面我們通過指定為電話號碼特定格式,然后鍵盤類型變成了撥號專用的鍵盤,這個是自動變的;

  其實我們也可以通過 android:inputType 來設置文本的類型,讓輸入法選擇合適的軟鍵盤;

  android: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"..................時間鍵盤

•設置最小行,最多行,單行,多行,自動換行

  • android:minLines="3" : 設置最小行的行數

  • android:maxLines="3" : 設置EditText最大的行數

    • 當輸入內容超過 maxline , 文字會自動向上滾動

  • android:singleLine="true" : 限制EditText只允許單行輸入,而且不會滾動

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

  • android:textScaleX="1.5" : 設置字與字的水平間隔

  • android:textScaleY="1.5" : 設置字與字的垂直間隔

  • android:capitalize=" " : 默認none,提供了三個可選值

    • sentences : 僅第一個字母大寫

    • words : 每一個單詞首字母大小,用空格區分單詞

    • characters : 每一個英文字母都大寫


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM