MainActivity如下:
package cc.textvIEw5; 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; import androID.app.Activity; /** * Demo描述: * 自定义控件实现带清除功能的EditText * * 学习资料: * http://blog.csdn.net/xiaanming/article/details/11066685 * * Thank you very much */ public class MainActivity extends Activity { private CleanableEditText mUsernameCleanableEditText; private CleanableEditText mPassWordCleanableEditText; private button mLoginbutton; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); init(); } private voID init(){ mUsernameCleanableEditText=(CleanableEditText) findVIEwByID(R.ID.usernameEditText); mPassWordCleanableEditText=(CleanableEditText) findVIEwByID(R.ID.passwordEditText); mLoginbutton=(button) findVIEwByID(R.ID.loginbutton); mLoginbutton.setonClickListener(new OnClickListenerImpl()); } private class OnClickListenerImpl implements OnClickListener { @OverrIDe public voID onClick(VIEw vIEw) { if (TextUtils.isEmpty(mUsernameCleanableEditText.getText())) { mUsernameCleanableEditText.setShakeAnimation(); Toast.makeText(MainActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show(); } if (TextUtils.isEmpty(mPassWordCleanableEditText.getText())) { mPassWordCleanableEditText.setShakeAnimation(); Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show(); } } } }
CleanableEditText如下:
package cc.textvIEw5; 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.animation.Animation; import androID.vIEw.animation.CycleInterpolator; import androID.vIEw.animation.TranslateAnimation; import androID.Widget.EditText; /** * 在焦点变化时和输入内容发生变化时均要判断是否显示右边clean图标 */ public class CleanableEditText extends EditText { private Drawable mRightDrawable; private boolean isHasFocus; public CleanableEditText(Context context) { super(context); init(); } public CleanableEditText(Context context,AttributeSet attrs) { super(context,attrs); init(); } public CleanableEditText(Context context,AttributeSet attrs,int defStyle) { super(context,attrs,defStyle); init(); } private voID init(){ //getCompoundDrawables: //Returns drawables for the left,top,right,and bottom borders. Drawable [] drawables=this.getCompoundDrawables(); //取得right位置的Drawable //即我们在布局文件中设置的androID:drawableRight mRightDrawable=drawables[2]; //设置焦点变化的监听 this.setonFocuschangelistener(new FocuschangelistenerImpl()); //设置EditText文字变化的监听 this.addTextChangedListener(new TextWatcherImpl()); //初始化时让右边clean图标不可见 setClearDrawableVisible(false); } /** * 当手指抬起的位置在clean的图标的区域 * 我们将此视为进行清除 *** 作 * getWIDth():得到控件的宽度 * event.getX():抬起时的坐标(改坐标是相对于控件本身而言的) * getTotalpaddingRight():clean的图标左边缘至控件右边缘的距离 * getpaddingRight():clean的图标右边缘至控件右边缘的距离 * 于是: * getWIDth() - getTotalpaddingRight()表示: * 控件左边到clean的图标左边缘的区域 * getWIDth() - getpaddingRight()表示: * 控件左边到clean的图标右边缘的区域 * 所以这两者之间的区域刚好是clean的图标的区域 */ @OverrIDe public boolean ontouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: boolean isClean =(event.getX() > (getWIDth() - getTotalpaddingRight()))&& (event.getX() < (getWIDth() - getpaddingRight())); if (isClean) { setText(""); } break; default: break; } return super.ontouchEvent(event); } private class FocuschangelistenerImpl implements OnFocuschangelistener{ @OverrIDe public voID onFocusChange(VIEw v,boolean hasFocus) { isHasFocus=hasFocus; if (isHasFocus) { boolean isVisible=getText().toString().length()>=1; setClearDrawableVisible(isVisible); } else { setClearDrawableVisible(false); } } } //当输入结束后判断是否显示右边clean的图标 private class TextWatcherImpl implements TextWatcher{ @OverrIDe public voID afterTextChanged(Editable s) { boolean isVisible=getText().toString().length()>=1; setClearDrawableVisible(isVisible); } @OverrIDe public voID beforeTextChanged(CharSequence s,int start,int count,int after) { } @OverrIDe public voID onTextChanged(CharSequence s,int before,int count) { } } //隐藏或者显示右边clean的图标 protected voID setClearDrawableVisible(boolean isVisible) { Drawable rightDrawable; if (isVisible) { rightDrawable = mRightDrawable; } else { rightDrawable = null; } //使用代码设置该控件left,and bottom处的图标 setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],rightDrawable,getCompoundDrawables()[3]); } // 显示一个动画,以提示用户输入 public voID setShakeAnimation() { this.setAnimation(shakeAnimation(5)); } //CycleTimes动画重复的次数 public Animation shakeAnimation(int CycleTimes) { Animation translateAnimation = new TranslateAnimation(0,10,10); translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes)); translateAnimation.setDuration(1000); return translateAnimation; } }
main.xml如下:
<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:paddingleft="20dip" androID:paddingRight="20dip" > <cc.textvIEw5.CleanableEditText androID:ID="@+ID/usernameEditText" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:hint="username" androID:layout_margintop="30dip" androID:drawableleft="@drawable/icon_user" androID:drawableRight="@drawable/clean_selector" /> <cc.textvIEw5.CleanableEditText androID:ID="@+ID/passwordEditText" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:hint="password" androID:password="true" androID:layout_margintop="100dip" androID:drawableleft="@drawable/account_icon" androID:drawableRight="@drawable/clean_selector" /> <button androID:ID="@+ID/loginbutton" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="Login" androID:layout_margintop="155dip" /> </relativeLayout>
以上就是对AndroID 带有删除按钮的EditText的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android 带有删除按钮的EditText全部内容,希望文章能够帮你解决Android 带有删除按钮的EditText所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)