public class defineEditText extends EditText {
    Context context;
    Drawable d;
public defineEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.context=context;
        inint();
    }

public defineEditText(Context context) {
        super(context);
        this.context=context;
        inint();
    }

    public defineEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context=context;
        inint();
    }

    @SuppressLint("NewApi")
    private void inint() {
        // TODO Auto-generated method stub
        addTextChangedListener(new TextWatcher() {
            
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                setdrawable();
            }

            
        });
    }
    private void setdrawable() {
        // TODO Auto-generated method stub
        if(getText().length()>1){
            d=context.getResources().getDrawable(R.drawable.ic_launcher);
            setCompoundDrawablesWithIntrinsicBounds(null, null, d, null);
        }else{
            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        canvas.drawLine(0, getHeight(), getWidth(), getHeight(), getPaint());
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if(d!=null&&event.getAction()==MotionEvent.ACTION_UP){
            if(event.getX()>getWidth()-50){
            setText("");}
        }
        return super.onTouchEvent(event);
    }

 

posted on 2014-04-09 19:17  青年程序猿  阅读(381)  评论(0编辑  收藏  举报