首页 > 技术文章 > 自定义组件之会抖动的edittext

gangmiangongjue 2015-07-06 14:48 原文

      废话休提,先上效果图。

    android自定义组件,所需重写的方法往往有:两参数的构造方法(布局加载必须)、ondraw()、ontouchevent()、onfocuschanged()等方法,不少的组件还需自定义attributeset。本组件的实现主要是借助ontouchevent、onfocuschanged和textwatcher以及一个自定义动画来实现的。

    主要思路是,在onfocuschanged和ontextchanged方法中通过getconpounddrawable()和setcompounddrawable()来控制按钮的显隐,再通过ontouch事件判断坐标,模拟点击事件,来控制清除文字,定义一个动画方法,点击提交调用即可。

    上代码:public class ClearEditText extends EditText implements OnFocusChangeListener,TextWatch

private Drawable mRightDrawable;
    public ClearEditText(Context context){
        this(context, null);
    }

    public ClearEditText(Context context, AttributeSet attrs) {
        this(context, attrs,android.R.attr.editTextStyle);
        // TODO Auto-generated constructor stub
    }
    public ClearEditText(Context context,AttributeSet attrs,int defStyle){
        super(context, attrs, defStyle);
        initClearEditText();
    }

    private void initClearEditText() {
        mRightDrawable = getCompoundDrawables()[2];
        if (mRightDrawable == null) {
            mRightDrawable = getResources().getDrawable(R.drawable.clear);
        }
        setRightDrawableVisible(false);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    private void setRightDrawableVisible(boolean b) {
        // TODO Auto-generated method stub
        Log.d("setVisible", "b's value is:"+String.valueOf(b));
        Drawable myFlagDrawable = b? mRightDrawable:null;
        if (myFlagDrawable == null) {
            Log.d("setVisible", "myflagDrawble is null");
        }
        else {
            myFlagDrawable.setBounds(0, 0, myFlagDrawable.getIntrinsicWidth(), myFlagDrawable.getIntrinsicHeight());//this is required or this is invisible
            Log.d("setVisible", "myflagDrawble is not null");
        }
        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], myFlagDrawable, getCompoundDrawables()[3]);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub
        Log.d("focus",String.valueOf(hasFocus) );
        if (hasFocus == true) {
            boolean visibility = getText().toString().length()>0 ? true : false;
            setRightDrawableVisible(visibility);
        }
        else {
            setRightDrawableVisible(hasFocus);
        }
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub

        float x = event.getX();
        //float y = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_UP:
            if (x>getWidth()-mRightDrawable.getIntrinsicWidth()&&x<getWidth()) {
                setText("");
            }
            break;

        default:
            break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        
    }
    @Override
      public void onTextChanged(CharSequence s, int start, int before, int count){
        Log.d("textchange","changed" );
          onFocusChange(this, true);
      };
      public void startShake(int counts){
         Animation animation = new TranslateAnimation(0, 10, 0 , 0);
         animation.setInterpolator(new CycleInterpolator(counts));//渐变器,位移动画帧数按照正弦函数重复变化 sinusoidal
animation.setDuration(
1000); startAnimation(animation); } }

在ontextchanged里边回调onfoucuschange,来设置删除按钮的显隐

推荐阅读