EditText 基本用法以及屬性

image.png
image.png
image.png

示例

<?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>

結果:

運行結果

這兩個輸入框的用的的大部分屬性都在上面的表格中了,我這里解決下沒有說過的屬性。
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方法中告訴服務器我要搜索的內容。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容