1. 程式人生 > 其它 >9(edittext)

9(edittext)

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

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>

效果:

 這裡解決下沒有說過的屬性
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>