您的位置:首页 > 移动开发 > Android开发

Android新的surpport支持库SurpportDesign之TextInputLayout

2016-07-14 10:27 302 查看

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 的使用方法 我这里也只是一个简单的使用。详细一些的我还没有深入研究。所以大家可以自行深入研究一下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息