0

我有一个如下图所示的场景:

在此处输入图像描述

如果用户在用户名或电子邮件字段中没有写任何内容,它将显示问号。另一方面,它会显示十字标记。怎么实现???

4

1 回答 1

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

您可以使用上面的代码来制作自定义视图

于 2013-05-09T09:37:26.143 回答