[Android]自己定义带删除输入框

白日放歌须纵酒,青春作伴好还乡。这篇文章主要讲述[Android]自己定义带删除输入框相关的知识,希望能为你提供帮助。
在项目开发中,带删除button输入框也是人们经常常使用到的,该文章便介绍一下怎样创建一个带删除输入框。当中,须要解决的问题例如以下:
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,则为其设置默认图片。然后再为输入框显示图片,便获得下图效果:
[Android]自己定义带删除输入框

文章图片

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 }


d)怎么监听是点击到那个删除图片的呢,这是一个值得思考的问题,在这里,有一种解决方式。那便是触点监听。依据点击的位置来推断是否在图片所处位置的范围内:

@Override public boolean onTouchEvent(MotionEvent event) { // 推断触碰是否结束 if (event.getAction() == MotionEvent.ACTION_UP) { // 推断所触碰的位置是否为清除的button 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); } }


ok。完整版的自己定义带删除输入框就全然实现了。为方便大家学习,下面为完整代码:

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); }// 将输入框默认设置为没有清除的button 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) { // 推断所触碰的位置是否为清除的button if (event.getX() > (getWidth() - getTotalPaddingRight()) & & event.getX() < (getWidth() - getPaddingRight())) { // 将editText里面的内容清除 setText(""); } } return super.onTouchEvent(event); }}




【[Android]自己定义带删除输入框】









    推荐阅读