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

Android开发-DesignDemo-AndroidStudio(十二)TextInputLayout

2016-11-07 11:43 615 查看
项目压缩包下载地址:http://download.csdn.net/detail/iwanghang/9675030



TextInputActivity.java:

package com.iwanghang.coordinatordemo;

import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;

public class TextInputActivity extends AppCompatActivity implements TextWatcher {
TextInputLayout text_input;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_input);
text_input = (TextInputLayout) findViewById(R.id.text_input);
// 监听editText的内容改变
// 实现 addTextChangedListener 的 beforeTextChanged onTextChanged
// afterTextChanged 这3个方法
text_input.getEditText().addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void afterTextChanged(Editable editable) {
if (editable.length()<6) {
text_input.setError("用户名必须为6位或以上");
text_input.setErrorEnabled(true);
} else {
text_input.setErrorEnabled(false);
}
}
}
activity_input_text.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"
android:id="@+id/activity_text_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.iwanghang.coordinatordemo.TextInputActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"/>
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"/>
</android.support.design.widget.TextInputLayout>

</LinearLayout>


项目压缩包下载地址:http://download.csdn.net/detail/iwanghang/9675030
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: