Hi,小夥伴們,Layout
學會了,Button
和TextView
學會了,ImageView
也學會了,是否是感受老是學習這些單一的東西稍微有點枯燥了呢?那麼學習了這篇文章以後,開始盡情發揮大家的想象力開始搞事情吧~java
這一篇咱們講解EditText
的使用,EditText
是一個輸入框,在Android開發中是經常使用的控件。也是獲取用戶數據的一種方式,EditText
是TextView
的子類,它繼承了TextView
的全部屬性。話很少說,讓咱們趕忙開始學習吧~android
EditText的結構git
public class EditText
extends TextViewapi
android.view.View
↳android.widget.TextView
↳ android.widget.EditText微信
1.默認提示文本的兩個屬性以下ide
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>
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,裏面有各類大神回答小夥伴們遇到的問題,咱們的微信羣立刻也將要和你們見面啦,屆時但願你們踊躍加入其中~~