EditText 基本用法



title: EditText 基本用法
tags: EditText,編輯框,輸入框

EditText介紹:

EditText 在開發中也是經常用到的控件,也是一個比較必要的組件,可以說它是用戶跟Android應用進行數據傳輸的窗戶,比如實現一個登陸界面,需要用戶輸入賬號密碼,然后我們獲取用戶輸入的內容,提交給服務器進行判斷。

EditText 支持的 XML 屬性及相關方法

XML 屬性 相關方法 說明
android:text setText(CharSequence text) 設置文本內容
android:textColor setTextColor(int color) 字體顏色
android:hint setHint(int resid) 內容為空時候顯示的文本
android:textColorHint void setHintTextColor(int color) 為空時顯示的文本的顏色
android:inputType setInputType(int type) 限制輸入類型
number:整數類型
numberDecimal:小數點類型
date:日期類型
text:文本類型(默認值)
phone:撥號鍵盤
textPassword:密碼
textVisiblePassword:可見密碼
textUri:網址
android:maxLength 限制顯示的文本長度,超出部分不顯示
android:minLines setMaxLines(int maxlines) 設置文本的最小行數
android:gravity setGravity(int gravity) 設置文本位置,如設置成“center”,文本將居中顯示。
android:drawableLeft setCompoundDrawables(Drawable left,Drawable top,Drawable right, Drawable bottom) 在text的左邊輸出一個drawable,如圖片
android:drawablePadding 設置text與drawable(圖片)的間隔,與drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可設置為負數,單獨使用沒有效果。
android:digits 設置允許輸入哪些字符。如“1234567890”
android:ellipsize 設置當文字過長時,該控件該如何顯示。
start:省略號顯示在開頭
end:省略號顯示在結尾
middle:省略號顯示在中間
marquee:以跑馬燈的方式顯示(動畫橫向移動)
android:lines setLines(int lines) 設置文本的行數,設置兩行就顯示兩行,即使第二行沒有數據。
android:lineSpacingExtra 設置行間距
android:singleLine setSingleLine() true:單行顯示 false:可以多行
android:textStyle 設置字形,可以設置一個或多個,用"|"隔開
bold:粗體
italic:斜體
bolditalic:又粗又斜

EditText實例:開發中常用的登錄界面

首先我們來看布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_phone"
        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:drawableLeft="@mipmap/icon_phone"
        android:drawableBottom="@drawable/shape_et_bottom_line"
        android:layout_marginTop="20dp"/>

    <EditText
        android:id="@+id/et_password"
        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:drawablePadding="10dp"
        android:hint="請輸入密碼"
        android:drawableBottom="@drawable/shape_et_bottom_line"
        android:drawableLeft="@mipmap/icon_password"/>

    <TextView
        android:id="@+id/tv_login"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="30dp"
        android:text="登 錄"
        android:textColor="#ffffffff"
        android:textSize="18sp" />
</LinearLayout>

運行效果圖如下:
帶有EditeText的登錄界面

這兩個輸入框的用的的大部分屬性都在上面的表格中了,我這里解決下沒有說過的屬性。
android:background="@null" 輸入框無背景
android:drawableBottom="@drawable/shape_et_bottom_line" 底部引入一個shape布局文件,這個布局文件就是輸入框的下划線。
shape_et_bottom_line.xml內容如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#1E7EE3" />
    <size android:height="1dp" android:width="500dp"/>

</shape>

EditeText還有哪些功能?

1.監聽用戶輸入的內容.
有這樣一個場景,一個搜索框,只要用戶輸入了內容就去請求服務器,於是我們在Activity里面監聽EditeText文本改變事件。

EditText etOne= (EditText) findViewById(R.id.et_phone);
etOne.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("Ansen","內容改變之前調用:"+s);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("Ansen","內容改變,可以去告訴服務器:"+s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i("Ansen","內容改變之后調用:"+s);
            }
 });

首先我們通過id找到EditText控件,並且添加監聽函數,內部內實現TextWatcher接口,重寫三個方法。我們可以在onTextChanged方法中告訴服務器我要搜索的內容。

源碼下載

點擊下載源碼

各位看官如果覺得文章不錯,幫忙點個贊吧,對於你來說是舉手之勞,但對於我來說這就是堅持下去的動力。

如果你想第一時間看我們的后期文章,掃碼關注公眾號,每周不定期推送Android開發實戰教程文章,你還等什么,趕快關注吧,學好技術,出任ceo,贏取白富美。。。。

      Android開發666 - 安卓開發技術分享
            掃描二維碼加關注

Android開發666


免責聲明!

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



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