android hint 自动消失

来源:互联网 发布:数控编程培训教程 编辑:程序博客网 时间:2024/05/22 14:35


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);
}
}
};

0 0
原创粉丝点击