TextinputLayout 是一個容器。 他主要是爲了處理Edittext 改變Edittext 的樣式 ,可以方便提示輸入校驗信息。提示輸入有誤或者其他信息
,TextInputLayout 關於是否只能接受一個子元素。我經過實驗是不是的。可以有多個元素 。
下面講下如何使用TExtInputLayout
1.添加依賴包 這個 可以查看我上一篇博客 關於Tablaout 已經說的很清楚了
2.xml部分代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.yu.viewpagertest.TextInputActivity">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/til"
android:layout_height="100dp"
app:theme="@style/AppTheme2"
>
<EditText
android:textColor="#000000"
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="輸入密碼"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
大家可以看到這裏就只是添加了一個 Edittext 但是我在TextInputLayout中添加了一個屬性 叫theme 那麼這個Theme是幹啥的他可以改變Edditext 控件的顏色 下面我貼出 Theme 的代碼
<style name="AppTheme2" >
<!-- Customize your theme here. -->
<item name="colorAccent">#000000</item>
</style>
這裏 colorAccent 就可以設置Edittext的下面的橫線的顏色 3Java代碼 部分
<span style="font-weight: bold; white-space: pre;"> </span>final TextInputLayout til= (TextInputLayout) findViewById(R.id.til);
EditText et= (EditText) findViewById(R.id.et);
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().endsWith("2")){
til.setError("");
}else{
til.setError("輸入有誤");
}
}
});
</pre><pre name="code" class="html"><span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>這裏就是一個簡單的校驗 輸入有誤 然後調用 TextInputLayout 的setError 提示錯誤信息。 如果輸入正確就取消錯誤提示
關於TextInputLayout 的使用方法 我這裏也只是一個簡單的使用。詳細一些的我還沒有深入研究。所以大家可以自行深入研究一下。