标签:android 自定义 带删除输入框 edittext
在项目开发中,带删除按钮输入框也是人们常常用到的,该文章便介绍一下如何创建一个带删除输入框。其中,需要解决的问题如下:
a)创建自定义editText类
b)在自定义editText中显示删除图片
c)根据输入框的输入情况显示或隐藏图片
d)点击删除图片文字消失,图片隐藏
e)根据输入框焦点失去和获得状态显示或隐藏图片
好了,问题明确了,开始实现功能:
a)创建一个名为MyClearEditText的class文件,并集成EditText,实现其构造方法:
public MyClearEditText(Context context) { this(context, null); // TODO Auto-generated constructor stub } public MyClearEditText(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.editTextStyle); // TODO Auto-generated constructor stub } public MyClearEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }ok,第一个问题解决了,进入第二步。
b)在editText中,我们若想显示上下左右方向的图片,有着setCompoundDrawables或setCompoundDrawablesWithIntrinsicBounds方法,具体的话,可以去百度一下其区别,在这里,我使用的是setCompoundDrawablesWithIntrinsicBounds方法。代码如下:
/** * 初始化清除的图片 */ private void initClearDrawable() { draw = getCompoundDrawables()[2]; // 判断清除的图片是否为空 if (draw == null) { draw = getResources().getDrawable(R.drawable.editdelete); } // 为输入框设置图片 this.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null); }思路为:先找到editText中右边的图片,若为null,则为其设置默认图片,然后再为输入框显示图片,便获得下图效果:
c)需要获得输入框的情况,便要实现TextWatcher接口。
监听:
this.addTextChangedListener(this);
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { // 判断输入框中是否有内容 if (text.length() > 0) { this.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null); } else { this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } public void afterTextChanged(Editable s) { // TODO Auto-generated method stub }
@Override public boolean onTouchEvent(MotionEvent event) { // 判断触碰是否结束 if (event.getAction() == MotionEvent.ACTION_UP) { // 判断所触碰的位置是否为清除的按钮 if (event.getX() > (getWidth() - getTotalPaddingRight()) && event.getX() < (getWidth() - getPaddingRight())) { // 将editText里面的内容清除 setText(""); } } return super.onTouchEvent(event); }
e)既然是根据焦点的得失来判断,当然是实现焦点监听的方法:
@Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { // TODO Auto-generated method stub super.onFocusChanged(focused, direction, previouslyFocusedRect); // 判断焦点失去和得到时的操作 if (focused && !this.getText().toString().equals("")) { this.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null); } else { this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); } }
package com.xiaoyan.xiaoyanlibrary.common.widget.edittext; import com.xiaoyan.xiaoyanlibrary.R; import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.EditText; /** * 自定义一个具有清除功能的editText * * @author xiejinxiong * */ public class MyClearEditText extends EditText implements TextWatcher { /** 储存清除的图片 */ private Drawable draw; public MyClearEditText(Context context) { this(context, null); // TODO Auto-generated constructor stub } public MyClearEditText(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.editTextStyle); // TODO Auto-generated constructor stub } public MyClearEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initClearDrawable(); this.addTextChangedListener(this); } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { // TODO Auto-generated method stub super.onFocusChanged(focused, direction, previouslyFocusedRect); // 判断焦点失去和得到时的操作 if (focused && !this.getText().toString().equals("")) { this.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null); } else { this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); } } /** * 初始化清除的图片 */ private void initClearDrawable() { draw = getCompoundDrawables()[2]; // 判断清除的图片是否为空 if (draw == null) { draw = getResources().getDrawable(R.drawable.editdelete); } // 将输入框默认设置为没有清除的按钮 this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); } public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { // 判断输入框中是否有内容 if (text.length() > 0) { this.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null); } else { this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Override public boolean onTouchEvent(MotionEvent event) { // 判断触碰是否结束 if (event.getAction() == MotionEvent.ACTION_UP) { // 判断所触碰的位置是否为清除的按钮 if (event.getX() > (getWidth() - getTotalPaddingRight()) && event.getX() < (getWidth() - getPaddingRight())) { // 将editText里面的内容清除 setText(""); } } return super.onTouchEvent(event); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:android 自定义 带删除输入框 edittext
原文地址:http://blog.csdn.net/u011596810/article/details/46970009