您的位置首页生活百科

Android带清除功能的输入框ClearEditText仿IOS

Android带清除功能的输入框ClearEditText仿IOS

的有关信息介绍如下:

Android带清除功能的输入框ClearEditText仿IOS

今天给大家带来一个很实用的小控件ClearEditText,就是在Android系统的输入框右边加入一个小图标,点击小图标可以清除输入框里面的内容,IOS上面直接设置某个属性就可以实现这一功能,但是Android原生EditText不具备此功能,所以要想实现这一功能我们需要重写EditText,接下来就带大家来实现这一小小的功能

我们知道,我们可以为我们的输入框在上下左右设置图片,所以我们可以利用属性android:drawableRight设置我们的删除小图标,如图

我这里设置了左边和右边的图片,如果我们能为右边的图片设置监听,点击右边的图片清除输入框的内容并隐藏删除图标,这样子这个小功能就迎刃而解了,可是Android并没有给允许我们给右边小图标加监听的功能,这时候你是不是发现这条路走不通呢,其实不是,我们可能模拟点击事件,用输入框的的onTouchEvent()方法来模拟,

当我们触摸抬起(就是ACTION_UP的时候)的范围 大于输入框左侧到清除图标左侧的距离,小与输入框左侧到清除图片右侧的距离,我们则认为是点击清除图片,当然我这里没有考虑竖直方向,只要给清除小图标就上了监听,其他的就都好处理了,我先把代码贴上来,在讲解下

今天给大家带来一个很实用的小控件ClearEditText,就是在Android系统的输入框右边加入一个小图标,点击小图标可以清除输入框里面的内容,IOS上面直接设置某个属性就可以实现这一功能,但是Android原生EditText不具备此功能,所以要想实现这一功能我们需要重写EditText,接下来就带大家来实现这一小小的功能

我们知道,我们可以为我们的输入框在上下左右设置图片,所以我们可以利用属性android:drawableRight设置我们的删除小图标,如图

我这里设置了左边和右边的图片,如果我们能为右边的图片设置监听,点击右边的图片清除输入框的内容并隐藏删除图标,这样子这个小功能就迎刃而解了,可是Android并没有给允许我们给右边小图标加监听的功能,这时候你是不是发现这条路走不通呢,其实不是,我们可能模拟点击事件,用输入框的的onTouchEvent()方法来模拟,

当我们触摸抬起(就是ACTION_UP的时候)的范围 大于输入框左侧到清除图标左侧的距离,小与输入框左侧到清除图片右侧的距离,我们则认为是点击清除图片,当然我这里没有考虑竖直方向,只要给清除小图标就上了监听,其他的就都好处理了,我先把代码贴上来,在讲解下

packagecom.example.clearedittext;

importandroid.content.Context;

importandroid.graphics.drawable.Drawable;

importandroid.text.Editable;

importandroid.text.TextWatcher;

importandroid.util.AttributeSet;

importandroid.view.MotionEvent;

importandroid.view.View;

importandroid.view.View.OnFocusChangeListener;

importandroid.view.animation.Animation;

importandroid.view.animation.CycleInterpolator;

importandroid.view.animation.TranslateAnimation;

importandroid.widget.EditText;

publicclassClearEditTextextendsEditTextimplements

OnFocusChangeListener,TextWatcher{

/**

*删除按钮的引用

*/

privateDrawablemClearDrawable;

/**

*控件是否有焦点

*/

privatebooleanhasFoucs;

publicClearEditText(Contextcontext){

this(context,null);

}

publicClearEditText(Contextcontext,AttributeSetattrs){

//这里构造方法也很重要,不加这个很多属性不能再XML里面定义

this(context,attrs,android.R.attr.editTextStyle);

}

publicClearEditText(Contextcontext,AttributeSetattrs,intdefStyle){

super(context,attrs,defStyle);

init();

}

privatevoidinit(){

//获取EditText的DrawableRight,假如没有设置我们就使用默认的图片

mClearDrawable=getCompoundDrawables();

if(mClearDrawable==null){

//thrownewNullPointerException("YoucanadddrawableRightattributeinXML");

mClearDrawable=getResources().getDrawable(R.drawable.delete_selector);

}

mClearDrawable.setBounds(0,0,mClearDrawable.getIntrinsicWidth(),mClearDrawable.getIntrinsicHeight());

//默认设置隐藏图标

setClearIconVisible(false);

//设置焦点改变的监听

setOnFocusChangeListener(this);

//设置输入框里面内容发生改变的监听

addTextChangedListener(this);

}

/**

*因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件

*当我们按下的位置在EditText的宽度-图标到控件右边的间距-图标的宽度和

*EditText的宽度-图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑

*/

@Override

publicbooleanonTouchEvent(MotionEventevent){

if(event.getAction()==MotionEvent.ACTION_UP){

if(getCompoundDrawables()!=null){

booleantouchable=event.getX()>(getWidth()-getTotalPaddingRight())

&&(event.getX()<((getWidth()-getPaddingRight())));

if(touchable){

this.setText("");

}

}

}

returnsuper.onTouchEvent(event);

}

/**

*当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏

*/

@Override

publicvoidonFocusChange(Viewv,booleanhasFocus){

this.hasFoucs=hasFocus;

if(hasFocus){

setClearIconVisible(getText().length()>0);

}else{

setClearIconVisible(false);

}

}

/**

*设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去

*@paramvisible

*/

protectedvoidsetClearIconVisible(booleanvisible){

Drawableright=visible?mClearDrawable:null;

setCompoundDrawables(getCompoundDrawables(),

getCompoundDrawables(),right,getCompoundDrawables());

}

/**

*当输入框里面内容发生变化的时候回调的方法

*/

@Override

publicvoidonTextChanged(CharSequences,intstart,intcount,

intafter){

if(hasFoucs){

setClearIconVisible(s.length()>0);

}

}

@Override

publicvoidbeforeTextChanged(CharSequences,intstart,intcount,

intafter){

}

@Override

publicvoidafterTextChanged(Editables){

}

/**

*设置晃动动画

*/

publicvoidsetShakeAnimation(){

this.setAnimation(shakeAnimation(5));

}

/**

*晃动动画

*@paramcounts1秒钟晃动多少下

*@return

*/

publicstaticAnimationshakeAnimation(intcounts){

AnimationtranslateAnimation=newTranslateAnimation(0,10,0,0);

translateAnimation.setInterpolator(newCycleInterpolator(counts));

translateAnimation.setDuration(1000);

returntranslateAnimation;

}

}

setClearIconVisible()方法,设置隐藏和显示清除图标的方法,我们这里不是调用setVisibility()方法,setVisibility()这个方法是针对View的,我们可以调用setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)来设置上下左右的图标

setOnFocusChangeListener(this)为输入框设置焦点改变监听,如果输入框有焦点,我们判断输入框的值是否为空,为空就隐藏清除图标,否则就显示

addTextChangedListener(this)为输入框设置内容改变监听,其实很简单呢,当输入框里面的内容发生改变的时候,我们需要处理显示和隐藏清除小图标,里面的内容长度不为0我们就显示,否是就隐藏,但这个需要输入框有焦点我们才改变显示或者隐藏,为什么要需要焦点,比如我们一个登陆界面,我们保存了用户名和密码,在登陆界面onCreate()的时候,我们把我们保存的密码显示在用户名输入框和密码输入框里面,输入框里面内容发生改变,导致用户名输入框和密码输入框里面的清除小图标都显示了,这显然不是我们想要的效果,所以加了一个是否有焦点的判断

setShakeAnimation(),这个方法是输入框左右抖动的方法,之前我在某个应用看到过类似的功能,当用户名错误,输入框就在哪里抖动,感觉挺好玩的,其实主要是用到一个移动动画,然后设置动画的变化率为正弦曲线

接下来我们来使用它,Activity的布局,两个我们自定义的输入框,一个按钮

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#95CAE4">

android:id="@+id/username"

android:layout_marginTop="60dp"

android:layout_width="fill_parent"

android:background="@drawable/login_edittext_bg"

android:drawableLeft="@drawable/icon_user"

android:layout_marginLeft="10dip"

android:layout_marginRight="10dip"

android:singleLine="true"

android:drawableRight="@drawable/delete_selector"

android:hint="输入用户名"

android:layout_height="wrap_content">

android:id="@+id/password"

android:layout_marginLeft="10dip"

android:layout_marginRight="10dip"

android:layout_marginTop="10dip"

android:drawableLeft="@drawable/account_icon"

android:hint="输入密码"

android:singleLine="true"

android:password="true"

android:drawableRight="@drawable/delete_selector"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@id/username"

android:background="@drawable/login_edittext_bg">

android:id="@+id/login"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="10dip"

android:layout_marginRight="10dip"

android:background="@drawable/login_button_bg"

android:textSize="18sp"

android:textColor="@android:color/white"

android:layout_below="@+id/password"

android:layout_marginTop="25dp"

android:text="登录"/>

然后就是界面代码的编写,主要是测试输入框左右晃动而已,比较简单

packagecom.example.clearedittext;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.text.TextUtils;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.Toast;

publicclassMainActivityextendsActivity{

privateToastmToast;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

finalClearEditTextusername=(ClearEditText)findViewById(R.id.username);

finalClearEditTextpassword=(ClearEditText)findViewById(R.id.password);

((Button)findViewById(R.id.login)).setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

if(TextUtils.isEmpty(username.getText())){

//设置晃动

username.setShakeAnimation();

//设置提示

showToast("用户名不能为空");

return;

}

if(TextUtils.isEmpty(password.getText())){

password.setShakeAnimation();

showToast("密码不能为空");

return;

}

}

});

}

/**

*显示Toast消息

*@parammsg

*/

privatevoidshowToast(Stringmsg){

if(mToast==null){

mToast=Toast.makeText(this,msg,Toast.LENGTH_SHORT);

}else{

mToast.setText(msg);

}

mToast.show();

}

}

运行项目,如图,你是不是也想把它加到你的项目当中去呢,赶紧吧!