
本文给大家带来一个很实用的@H_301_1@小控件ClearEditText,就是在AndroID系统的输入框右边加入一个小图标,点击小图标可以清除输入框里面的内容,IOS上面直接设置某个属性就可以实现这一功能,但是AndroID原生EditText不具备此功能,所以要想实现这一功能我们需要重写EditText,接下来就带大家来实现这一小小的功能
我们知道,我们可以为我们的输入框在上下左右设置图片,所以我们可以利用属性androID:drawableRight设置我们的删除小图标,如图
我这里设置了左边和右边的图片,如果我们能为右边的图片设置监听,点击右边的图片清除输入框的内容并隐藏删除图标,这样子这个小功能就迎刃而解了,可是AndroID并没有给允许我们给右边小图标加监听的功能,这时候你是不是发现这条路走不通呢,其实不是,我们可能模拟点击事件,用输入框的的ontouchEvent()方法来模拟,
当我们触摸抬起(就是ACTION_UP的时候)的范围 大于输入框左侧到清除图标左侧的距离,小与输入框左侧到清除图片右侧的距离,我们则认为是点击清除图片,当然我这里没有考虑竖直方向,只要给清除小图标就上了监听,其他的就都好处理了,我先把代码贴上来,在讲解下
package com.example.clearedittext; import androID.content.Context; import androID.graphics.drawable.Drawable; import androID.text.Editable; import androID.text.TextWatcher; import androID.util.AttributeSet; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnFocuschangelistener; import androID.vIEw.animation.Animation; import androID.vIEw.animation.CycleInterpolator; import androID.vIEw.animation.TranslateAnimation; import androID.Widget.EditText; public class ClearEditText extends EditText implements OnFocuschangelistener,TextWatcher { /** * 删除按钮的引用 */ private Drawable mClearDrawable; /** * 控件是否有焦点 */ private boolean hasFoucs; public ClearEditText(Context context) { this(context,null); } public ClearEditText(Context context,AttributeSet attrs) { //这里构造方法也很重要,不加这个很多属性不能再XML里面定义 this(context,attrs,androID.R.attr.editTextStyle); } public ClearEditText(Context context,AttributeSet attrs,int defStyle) { super(context,defStyle); init(); } private voID init() { //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片 mClearDrawable = getCompoundDrawables()[2]; if (mClearDrawable == null) { // throw new NullPointerException("You can add drawableRight attribute in XML"); mClearDrawable = getResources().getDrawable(R.drawable.delete_selector); } mClearDrawable.setBounds(0,mClearDrawable.getIntrinsicWIDth(),mClearDrawable.getIntrinsicHeight()); //默认设置隐藏图标 setClearIconVisible(false); //设置焦点改变的监听 setonFocuschangelistener(this); //设置输入框里面内容发生改变的监听 addTextChangedListener(this); } /** * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件 * 当我们按下的位置 在 EditText的宽度 - 图标到控件右边的间距 - 图标的宽度 和 * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑 */ @OverrIDe public boolean ontouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (getCompoundDrawables()[2] != null) { boolean touchable = event.getX() > (getWIDth() - getTotalpaddingRight()) && (event.getX() < ((getWIDth() - getpaddingRight()))); if (touchable) { this.setText(""); } } } return super.ontouchEvent(event); } /** * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏 */ @OverrIDe public voID onFocusChange(VIEw v,boolean hasFocus) { this.hasFoucs = hasFocus; if (hasFocus) { setClearIconVisible(getText().length() > 0); } else { setClearIconVisible(false); } } /** * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去 * @param visible */ protected voID setClearIconVisible(boolean visible) { Drawable right = visible ? mClearDrawable : null; setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],right,getCompoundDrawables()[3]); } /** * 当输入框里面内容发生变化的时候回调的方法 */ @OverrIDe public voID onTextChanged(CharSequence s,int start,int count,int after) { if(hasFoucs){ setClearIconVisible(s.length() > 0); } } @OverrIDe public voID beforeTextChanged(CharSequence s,int after) { } @OverrIDe public voID afterTextChanged(Editable s) { } /** * 设置晃动动画 */ public voID setShakeAnimation(){ this.setAnimation(shakeAnimation(5)); } /** * 晃动动画 * @param counts 1秒钟晃动多少下 * @return */ public static Animation shakeAnimation(int counts){ Animation translateAnimation = new TranslateAnimation(0,10,0); translateAnimation.setInterpolator(new CycleInterpolator(counts)); translateAnimation.setDuration(1000); return translateAnimation; } } @H_301_1@setClearIconVisible()方法,设置隐藏和显示清除图标的方法,我们这里不是调用setVisibility()方法,setVisibility()这个方法是针对VIEw的,我们可以调用setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom)来设置上下左右的图标
@H_301_1@setonFocuschangelistener(this) 为输入框设置焦点改变监听,如果输入框有焦点,我们判断输入框的值是否为空,为空就隐藏清除图标,否则就显示
@H_301_1@addTextChangedListener(this) 为输入框设置内容改变监听,其实很简单呢,当输入框里面的内容发生改变的时候,我们需要处理显示和隐藏清除小图标,里面的内容长度不为0我们就显示,否是就隐藏,但这个需要输入框有焦点我们才改变显示或者隐藏,为什么要需要焦点,比如我们一个登陆界面,我们保存了用户名和密码,在登陆界面onCreate()的时候,我们把我们保存的密码显示在用户名输入框和密码输入框里面,输入框里面内容发生改变,导致用户名输入框和密码输入框里面的清除小图标都显示了,这显然不是我们想要的效果,所以加了一个是否有焦点的判断
@H_301_1@setShakeAnimation(),这个方法是输入框左右抖动的方法,之前我在某个应用看到过类似的功能,当用户名错误,输入框就在哪里抖动,感觉挺好玩的,其实主要是用到一个移动动画,然后设置动画的变化率为正弦曲线
接下来我们来使用它,Activity的布局,两个我们自定义的输入框,一个按钮
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#95CAE4"> <com.example.clearedittext.ClearEditText 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" > </com.example.clearedittext.ClearEditText> <com.example.clearedittext.ClearEditText 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" > </com.example.clearedittext.ClearEditText> <button 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="登录" /> </relativeLayout>
然后就是界面代码的编写,主要是测试输入框左右晃动而已,比较简单
package com.example.clearedittext; import androID.app.Activity; import androID.os.Bundle; import androID.text.TextUtils; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.Toast; public class MainActivity extends Activity { private Toast mToast; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); final ClearEditText username = (ClearEditText) findVIEwByID(R.ID.username); final ClearEditText password = (ClearEditText) findVIEwByID(R.ID.password); ((button) findVIEwByID(R.ID.login)).setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if(TextUtils.isEmpty(username.getText())){ //设置晃动 username.setShakeAnimation(); //设置提示 showToast("用户名不能为空"); return; } if(TextUtils.isEmpty(password.getText())){ password.setShakeAnimation(); showToast("密码不能为空"); return; } } }); } /** * 显示Toast消息 * @param msg */ private voID showToast(String msg){ if(mToast == null){ mToast = Toast.makeText(this,msg,Toast.LENGTH_SHORT); }else{ mToast.setText(msg); } mToast.show(); } } 运行项目,如图,悲剧,没有动画效果,算了就这样子吧,你是不是也想把它加到你的项目当中去呢,赶紧吧!
以上就是本文的全部内容,希望对大家学习AndroID软件编程有所帮助。
总结以上是内存溢出为你收集整理的Android输入框控件ClearEditText实现清除功能全部内容,希望文章能够帮你解决Android输入框控件ClearEditText实现清除功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)