Android 自定义EditText清除功能输入
2017-11-08 14:08
375 查看
1.创建一个类(用来自定义控件)ClearEditText 2.在布局中使用 包名+类名 <com.llew.e.clear.view.wedgit.ClearEditText android:id="@+id/clearEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip" android:paddingRight="8dip" android:paddingTop="5dip" android:paddingBottom="5dip" android:hint="请输入QQ号码" android:drawableLeft="@drawable/super_qq" //左边的图片 android:drawableRight="@drawable/clear_normal_list" /> //清除的图片 public class ClearEditText extends EditText implements TextWatcher, OnFocusChangeListener { /** * 左右两侧图片资源 */ private Drawable left, right; /** * 是否获取焦点,默认没有焦点 */ private boolean hasFocus = false; /** * 手指抬起时的X坐标 */ private int xUp = 0; 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 defStyle) { super(context, attrs, defStyle); initWedgits(attrs); } /** * 初始化各组件 * @param attrs * 属性集 */ private void initWedgits(AttributeSet attrs) { try { left = getCompoundDrawables()[0]; right = getCompoundDrawables()[2]; initDatas(); } catch (Exception e) { e.printStackTrace(); } } /** * 初始化数据 */ private void initDatas() { try { // 第一次显示,隐藏删除图标 setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); addListeners(); } catch (Exception e) { e.printStackTrace(); } } /** * 添加事件监听 */ private void addListeners() { try { setOnFocusChangeListener(this); addTextChangedListener(this); } catch (Exception e) { e.printStackTrace(); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int after) { if (hasFocus) { if (TextUtils.isEmpty(s)) { // 如果为空,则不显示删除图标 setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); } else { // 如果非空,则要显示删除图标 if (null == right) { right = getCompoundDrawables()[2]; } setCompoundDrawablesWithIntrinsicBounds(left, null, right, null); } } } @Override public boolean onTouchEvent(MotionEvent event) { try { switch (event.getAction()) { case MotionEvent.ACTION_UP: // 获取点击时手指抬起的X坐标 xUp = (int) event.getX(); // 当点击的坐标到当前输入框右侧的距离小于等于getCompoundPaddingRight()的距离时,则认为是点击了删除图标 // getCompoundPaddingRight()的说明:Returns the right padding of the view, plus space for the right Drawable if any. if ((getWidth() - xUp) <= getCompoundPaddingRight()) { if (!TextUtils.isEmpty(getText().toString())) { setText(""); } } break; default: break; } } catch (Exception e) { e.printStackTrace(); } return super.onTouchEvent(event); } @Override public void afterTextChanged(Editable s) { } @Override public void onFocusChange(View v, boolean hasFocus) { try { this.hasFocus = hasFocus; } catch (Exception e) { e.printStackTrace(); } } } |
相关文章推荐
- Android 带清除功能的输入框控件EditText
- (一、有清除功能的 EditText扩展组件)android 自定义组件摸索过程中的一点分享
- Android自定义具有清除输入的EditText
- Android中自定义EditText控件(自带清除功能等)
- Android UI设计系列之自定义EditText实现带清除功能的输入框(3)
- Android 带清除功能的输入框控件EditText
- Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
- Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
- Android 带清除功能的输入框控件EditTextWithDel
- Android 带清除功能的输入框控件EditTextWithDel
- Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
- Android自定义组合控件--EditText和Button组合成带有清空EditText内容功能的复合控件
- 自定义EditText—带有清除功能,在显示多行文本时清除按钮位于整个EditText的右下角
- Android 带清除 和 晃动效果的 自定义 EditText 解析
- Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
- androidUI设计之旅 ----自定义EditText(多了一个清除按钮)
- Android 带清除功能的输入框控件ClearEditText
- Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
- Android EditText输入最大值提示功能
- Android设置EditText显示输入的表情Icon(自定义的聊天有个发送表情功能,在输入框显示表情)