android hint 自动消失 .
2015-12-31 19:03
411 查看
android EditText有个hint属性,可以在用户没有选择输入框时给予提示
但是这个提示必须是在用户有输入字符后才会消失,似乎不太符合国人习惯,有时还会误导,所以要让用户点击到输入框时hint文本就自动消失,方法是监听焦点事件:
写一个公用的方法:
public static OnFocusChangeListener onFocusAutoClearHintListener = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
EditText textView = (EditText) v;
String hint;
if (hasFocus) {
hint = textView.getHint().toString();
textView.setTag(hint);
textView.setHint("");
} else {
hint = textView.getTag().toString();
textView.setHint(hint);
}
}
};
给输入框绑定事件:
EditText loginNameTxt = (EditText) findViewById(R.id.loginNameTxt);
loginNameTxt.setOnFocusChangeListener(PublicFunc.onFocusAutoClearHintListener);
android EditText有个hint属性,可以在用户没有选择输入框时给予提示
但是这个提示必须是在用户有输入字符后才会消失,似乎不太符合国人习惯,有时还会误导,所以要让用户点击到输入框时hint文本就自动消失,方法是监听焦点事件:
写一个公用的方法:
public static OnFocusChangeListener onFocusAutoClearHintListener = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
EditText textView = (EditText) v;
String hint;
if (hasFocus) {
hint = textView.getHint().toString();
textView.setTag(hint);
textView.setHint("");
} else {
hint = textView.getTag().toString();
textView.setHint(hint);
}
}
};
给输入框绑定事件:
EditText loginNameTxt = (EditText) findViewById(R.id.loginNameTxt);
loginNameTxt.setOnFocusChangeListener(PublicFunc.onFocusAutoClearHintListener);
相关文章推荐
- android Graphics:区域(Range)
- 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性
- Android 版本及别名
- [Android]使用Dagger 2依赖注入 - API(翻译)
- Android Support兼容包详解使用
- Android代码风格
- Android学习笔记:使用SurfaceView制作简单游戏(1)
- android 学习中遇到的问题及解决方法
- Android 应用出错 error: Error retrieving parent for item: No resource found that matches the的第二种情况
- 如何为Google的Nexus 5手机编译Android KitKat(4.4)
- Android Design - Photoshop 基本配置(1) - 画布大小
- ubuntu android studio 编译及运行错误Error retrieving parent for item: No resource found that matches the given name
- Android桌面小插件——Widget
- Android桌面小插件——Widget
- Android添加phonegap--文件上传插件教程
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- Android之JAVASe基础篇-面向对象-类集(十)
- Android Studio Plugins
- android 模拟抢红包 原理
- Android开源之五---优秀个人和团体篇