您的位置:首页 > 移动开发 > Android开发

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();
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: