我有一个如下图所示的场景:
如果用户未在“用户名”或“电子邮件”字段中写入任何内容,则会显示问号。另一方面,它将显示十字标记。怎么实现呢???
答案 0 :(得分:1)
etUsername.addTextChangedListener(mTextWatcher);
etUsername.setOnTouchListener(new MyOnTouchListener(etUsername));
TextWatcher :按用户输入显示或隐藏清除按钮
private TextWatcher mTextWatcher = new TextWatcher() {
boolean isnull = true;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if(TextUtils.isEmpty(s)){
if(!isnull){
etUsername.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
isnull = true;
}
}else{
if(isnull){
etUsername.setCompoundDrawablesWithIntrinsicBounds(null, null, mClearIcon, null);
isnull = false;
}
}
}
MyOnTouchListener :清除按钮单击侦听器
class MyOnTouchListener implements OnTouchListener{
EditText mEditText;
public MyOnTouchListener(EditText editText){
this.mEditText = editText;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_UP:
int curX = (int)event.getX();
if(curX > v.getWidth() - 60
&& !TextUtils.isEmpty(mEditText.getText())){
// the clear button was clicked,do something you need
// for example, show the hint msg,etc
return true;
}
break;
}
return false;
}
您可以使用上面的代码制作自定义视图