android EditText里面的文字个数变化,提示自动取消
2015-08-10 16:58
525 查看
EditText里面的文字变化的时候,经常会出现一下提示字符,如输入错误密码,会提示密码错误,当我们点击EditText,修改密码,里面的文字个数有变动的时候,希望下面的这行字消失。用TextWatcher可以解决。TextWatcher为编辑框监听器。
在layout里面创建activity_main.xml文件。
<LinearLayout 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"
android:orientation="vertical"
android:background="#f4f4f4">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:inputType="number"
android:maxLength="8"
android:text="12345"
android:textColor="#000000"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="5dp"
android:text="你输入的密码有误"
android:textColor="#ff5077" />
</LinearLayout>
创建TextViewWatcher继承自TextWatcher。beforeTextChanged可以给出变化之前的内容,onTextChanged和afterTextChanged给出追加上新的字符之后的文本;
public class TextViewWatcher implements TextWatcher {
private TextView view;
public TextViewWatcher(TextView v) {
view = v;
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void o
4000
nTextChanged(CharSequence s, int start, int before, int count) {
view.setVisibility(View.INVISIBLE);
}
}
在MainActivity里面的onCreate直接调用
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.edit);
text=(TextView)findViewById(R.id.text);
edit.addTextChangedListener(new TextViewWatcher(text));
}
如图,当有文字变化时下面的提示自动消失。
在layout里面创建activity_main.xml文件。
<LinearLayout 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"
android:orientation="vertical"
android:background="#f4f4f4">
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:inputType="number"
android:maxLength="8"
android:text="12345"
android:textColor="#000000"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="5dp"
android:text="你输入的密码有误"
android:textColor="#ff5077" />
</LinearLayout>
创建TextViewWatcher继承自TextWatcher。beforeTextChanged可以给出变化之前的内容,onTextChanged和afterTextChanged给出追加上新的字符之后的文本;
public class TextViewWatcher implements TextWatcher {
private TextView view;
public TextViewWatcher(TextView v) {
view = v;
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void o
4000
nTextChanged(CharSequence s, int start, int before, int count) {
view.setVisibility(View.INVISIBLE);
}
}
在MainActivity里面的onCreate直接调用
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.edit);
text=(TextView)findViewById(R.id.text);
edit.addTextChangedListener(new TextViewWatcher(text));
}
如图,当有文字变化时下面的提示自动消失。
相关文章推荐
- Android-RecylerView初识
- Android-- 高仿QQ5.2双向侧滑菜单 使用DrawerLayout
- Android 手势检测实战 打造支持缩放平移的图片预览效果(下)
- Android 的事件传递机制,详细解释
- Android常用存储类型与简介
- android-Ultra-Pull-To-Refresh使用教程集锦
- Android--高仿 QQ5.0 侧滑菜单效果 自定义控件
- Android--自定义控件打造史上最简单的侧滑菜单
- (4.1.23.0)Android Animation学习笔记及位移动画的P属性图示
- android:contentDescription
- Android中RelativeLayout各个属性的含义
- Android AsyncTask 解析
- Android PopupWindow的使用和分析
- Android 权限大全~
- Android的5样的调试信息
- Android RecyclerView 使用完全解析 体验艺术般的控件
- Proguard 简介
- android调用系统相机和相册进行拍照裁剪处理,解决不同安卓版本存在无法加载相册的问题,处理了是否有sd卡的存在的情况
- Android 非空格式验证框架
- Android 手势检测实战 打造支持缩放平移的图片预览效果(上)