Android 带清除功能的输入框控件ClearEditText

来源:互联网 发布:centos 怎么读 编辑:程序博客网 时间:2024/06/04 18:50

转载地址。如有转载请标明地址

 demo下载:http://download.csdn.net/detail/day_moon/9664470

效果图:


ClearEditText类的定义
public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {    private Drawable mClearDrawable;//删除按钮    private boolean hasFoucs;//是否有焦点    public ClearEditText(Context context) {        this(context, null);    }    public ClearEditText(Context context, AttributeSet attrs) {        this(context, attrs, android.R.attr.editTextStyle);  //不加该属性,不能在配置文件里面定义    }    public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    private void init() {        mClearDrawable = getCompoundDrawables()[2];//获取删除的图片        if (mClearDrawable == null) {            mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);        }        mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());        setClearIconVisible(false);        setOnFocusChangeListener(this);        addTextChangedListener(this);    }    public boolean onTouchEvent(MotionEvent event) {        if (event.getAction() == MotionEvent.ACTION_UP) {            if (getCompoundDrawables()[2] != null) {                boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())                        && (event.getX() < (getWidth() - getPaddingRight()));                if (touchable) {                    this.setText("");                }            }        }        return super.onTouchEvent(event);    }    @Override    public void onFocusChange(View v, boolean hasFocus) {        this.hasFoucs = hasFocus;        if (hasFocus) {            setClearIconVisible(getText().length() > 0);        } else {            setClearIconVisible(false);        }    }    public void setClearIconVisible(boolean visible) {        Drawable right = visible ? mClearDrawable : null;        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);    }    public void beforeTextChanged(CharSequence s, int start, int count, int after) {    }    public void afterTextChanged(Editable s) {    }    /**     * 当输入框里面内容发生变化的时候回调的方法     */    @Override    public void onTextChanged(CharSequence s, int start, int count, int after) {        if (hasFoucs) {            setClearIconVisible(s.length() > 0);        }    }    public void setShakeAnimation() {        this.setAnimation(shakeAnimation(5));//设置晃动动画    }    private static Animation shakeAnimation(int counts) {        Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);        translateAnimation.setInterpolator(new CycleInterpolator(counts));        translateAnimation.setDuration(1000);        return translateAnimation;    }}
MainActivity 主类:
public class MainActivity extends Activity {    private Toast mToast;    public Button bt_login;    public ClearEditText username, password;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        username = (ClearEditText) findViewById(R.id.username);        password = (ClearEditText) findViewById(R.id.password);        bt_login = (Button) findViewById(R.id.login);        bt_login.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                if (TextUtils.isEmpty(username.getText())){                    username.setShakeAnimation();                    Toast.makeText(MainActivity.this,"用户名不能为空!",Toast.LENGTH_LONG).show();                    return;                }                if(TextUtils.isEmpty(password.getText())){                    password.setShakeAnimation();                    Toast.makeText(MainActivity.this,"密码不能为空!",Toast.LENGTH_LONG).show();                    return;                }            }        });    }}
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent">    <view.ClearEditText        android:id="@+id/username"        android:layout_marginTop="60dp"        android:layout_width="fill_parent"        android:drawableLeft="@mipmap/icon_user"        android:layout_marginLeft="10dip"        android:layout_marginRight="10dip"        android:singleLine="true"        android:drawableRight="@drawable/delete_selector"        android:hint="输入用户名"        android:layout_height="wrap_content" >    </view.ClearEditText>    <view.ClearEditText        android:id="@+id/password"        android:layout_marginLeft="10dip"        android:layout_marginRight="10dip"        android:layout_marginTop="10dip"        android:drawableLeft="@mipmap/account_icon"        android:hint="输入密码"        android:singleLine="true"        android:password="true"        android:drawableRight="@drawable/delete_selector"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/username">    </view.ClearEditText>    <Button        android:id="@+id/login"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dip"        android:layout_marginRight="10dip"        android:textSize="18sp"        android:textColor="@android:color/white"        android:layout_below="@+id/password"        android:layout_marginTop="25dp"        android:text="登录" /></RelativeLayout>




0 0
原创粉丝点击