Android的ClearEdtitext自定义封装class
来源:互联网 发布:11登陆不上 网络异常 编辑:程序博客网 时间:2024/05/23 02:04
对于android的Edittext控件的自定义,常常会用到带有删除功能的Edittext,我们对此功能进行封装,然后在对Edittext控件进行编写的时候直接进行使用即可。
废话不多说,实现以后的效果图如下所示:
对ClearEditText视图类进行一个封装,代码:
import android.content.Context;import android.graphics.drawable.Drawable;import android.text.Editable;import android.text.TextWatcher;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.View.OnFocusChangeListener;import android.view.animation.Animation;import android.view.animation.CycleInterpolator;import android.view.animation.TranslateAnimation;import android.widget.EditText;/** * Created by Administrator on 2016/7/13 0013. */public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher { /** * 删除按钮的引用 */ private Drawable mClearDrawable; /** * 控件是否有焦点 */ private boolean hasFoucs; public ClearEditText(Context context) { this(context, null); } public ClearEditText(Context context, AttributeSet attrs) { //这里构造方法也很重要,不加这个很多属性不能再XML里面定义 this(context, attrs, android.R.attr.editTextStyle); } public ClearEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片 mClearDrawable = getCompoundDrawables()[2]; if (mClearDrawable == null) {// throw new NullPointerException("You can add drawableRight attribute in XML"); mClearDrawable = getResources().getDrawable(R.mipmap.btn); } mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); //默认设置隐藏图标 setClearIconVisible(false); //设置焦点改变的监听 setOnFocusChangeListener(this); //设置输入框里面内容发生改变的监听 addTextChangedListener(this); } /** * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件 * 当我们按下的位置 在 EditText的宽度 - 图标到控件右边的间距 - 图标的宽度 和 * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑 */ @Override 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); } /** * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏 */ @Override public void onFocusChange(View v, boolean hasFocus) { this.hasFoucs = hasFocus; if (hasFocus) { setClearIconVisible(getText().length() > 0); } else { setClearIconVisible(false); } } /** * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去 * * @param visible */ protected void setClearIconVisible(boolean visible) { Drawable right = visible ? mClearDrawable : null; setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]); } /** * 当输入框里面内容发生变化的时候回调的方法 */ @Override public void onTextChanged(CharSequence s, int start, int count, int after) { if (hasFoucs) { setClearIconVisible(s.length() > 0); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } /** * 设置晃动动画 */ public void setShakeAnimation() { this.setAnimation(shakeAnimation(5)); } /** * 晃动动画 * * @param counts 1秒钟晃动多少下 * @return */ public static Animation shakeAnimation(int counts) { Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); translateAnimation.setInterpolator(new CycleInterpolator(counts)); translateAnimation.setDuration(1000); return translateAnimation; }}
封装好以后,我们在xml进行布局文件的编写的时候,就可以直接引用了,xml如下所示:
<com.test.view.ClearEditText android:id="@+id/input_testcard" android:layout_width="255dp" android:layout_height="50dp" android:layout_centerHorizontal="true" android:layout_marginTop="55dp" android:background="@drawable/test_edit_select" android:hint="@string/test_card" android:maxLength="4" android:numeric="integer" android:paddingLeft="54dp" android:paddingRight="17dp" android:singleLine="true" android:textColor="#de6654" android:textColorHint="#8e8e8e" android:textSize="14sp" />
这样使用就实现了我们的Edittext加clear功能了。
0 0
- Android的ClearEdtitext自定义封装class
- class类的封装
- Android 自定义dialog(封装好的)
- Android之自定义View的封装
- android 自定义View对Title的封装
- 一个android自定义对话框的封装
- Android 自定义通用Dialog的封装
- android Dialog 自定义 封装
- Android自定义控件封装之自定义属性的实现
- 获取class函数的封装
- 自定义SharedPreferences的封装
- okhttp的自定义封装
- js 封装获取class的值、添加class、删除class
- Android:图解classloader加载class的流程及自定义ClassLoader
- Android自定义控件之app标题栏的封装
- Android自定义控件:将ViewPager封装自己的TabPager控件
- 浅谈android中的自定义封装易用的Dialog
- Android自定义网络数据加载等待框的简单封装
- Git使用记录
- 子集生成算法
- centos7 mysql: mariadb-wait-ready $MAINPID (code=exited, status=1/FAILURE)
- poj 几道简单的搜索题 (一)
- 装windows和Linux系统时找不到硬盘,pe安装系统没有出现磁盘,不能识别磁盘
- Android的ClearEdtitext自定义封装class
- 关于angularjs中$http POST请求参数的问题
- Java:myeclipse连接MySQL的ODBC详细步骤
- 解析xml
- Android的新特性RecycleView
- 如何快速做饭?
- linux下简单对拍shell脚本
- fread
- caffe DB类学习