- 效果如下:
20170207094559319.png
20170207094616679.png
TextInputLayout 控件表現(xiàn)得就像LinearLayout 一樣氨肌,它是一個容器汞扎。TextInputLayout 中只能放一個子元素国瓮,和ScrollView有點(diǎn)類似茂蚓,并且子元素必須是EditText押赊;
TextInputLayout 實(shí)現(xiàn)的功能就是當(dāng)EditText中輸入第一個字母要隱藏hint的時候捞挥,TextInputLayout中會出現(xiàn)一個懸浮的標(biāo)簽來顯示這個hint兼蕊,還負(fù)責(zé)一個炫酷的的material 動畫
- 首先是怎樣引入TextInputLayout
compile 'com.android.support:design:25.1.0'
- 布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@mipmap/login_bg"
tools:context="cn.hnshangyu.textinputlayout.MainActivity">
<android.support.design.widget.TextInputLayout
app:hintTextAppearance="@style/FloatingStyle"
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<EditText
android:layout_width="260dp"
android:layout_height="wrap_content"
android:inputType="phone"
android:maxLines="1"
android:padding="@dimen/padding_10"
android:textColor="@color/white70"
android:textColorHint="@color/white70" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="@id/username"
android:layout_marginTop="@dimen/padding_20">
<EditText
android:layout_width="260dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:maxLines="1"
android:padding="@dimen/padding_10"
android:textColor="@color/white70"
android:textColorHint="@color/white70" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
在這里可以看到 app:hintTextAppearance="@style/FloatingStyle"這個屬性果正,這個屬性是干嘛的呢秦躯?
其實(shí)當(dāng)在TextInputLayout中設(shè)置這個屬性后忆谓,就會讓hint字體的內(nèi)容固定在EditText上方不會出現(xiàn)動畫效果,同時hint字體顏色變化在FloatingStyle中設(shè)置踱承!如效果圖中的手機(jī)號輸入框...
- FloatingStyle的內(nèi)容:
<style name="FloatingStyle" parent="@android:style/TextAppearance">
<item name="android:textColor">#e0c9c4c4</item>
<item name="android:textSize">12sp</item>
</style>
- 在MainActivity中對TextInputLayout進(jìn)行設(shè)置
package cn.hnshangyu.textinputlayout;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@Bind(R.id.username)
TextInputLayout username;
@Bind(R.id.password)
TextInputLayout password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
username.setHint(getResources().getString(R.string.phone_num));
password.setHint(getResources().getString(R.string.input_password));
username.setError(getResources().getString(R.string.phone_num_error));
}
}
TextInputLayout既可以在輸入時把提示語顯示與EditText的上方倡缠,又能把錯誤信息提示顯示在EditText的下方。
TextInputLayout需要注意的幾點(diǎn):
TextInputLayout不能單獨(dú)使用茎活,必須和EditText嵌套使用昙沦。
1個TextInputLayout只能包1個EditTExt。
再來看看比較常用的方法
1载荔、setHint();設(shè)置提示語
2盾饮、setError();設(shè)置錯誤顯示信息
3、setErrorEnabled();設(shè)置錯誤信息是否顯示懒熙。true顯示丘损,false不顯示。
4工扎、getEditText();得到EditText的控件實(shí)例徘钥。
注意:
不用設(shè)置setErrorEnabled()為true也是可以的。因?yàn)檫@里會調(diào)用setErrorEnabled(true)定庵。