Android实战简易教程-第三十二枪(自定义View登录注册界面EditText-实现一键清空)
2015-08-11 22:20
1086 查看
自定义View实现登录注册页面的EditText一键清空功能,效果如下:
输入框输入文字后自动出现一键清空键,输入框文字为空时,一键清空键隐藏,下面我们看一下如何通过自定义View实现这一效果。
看一下DeletableEditText.java:
布局文件代码:
运行实例就可以得到上图的效果,可以实现一键清空。当然里面也写了动画,判断用户是否输入内容,没有输入内容时,可以通过动画提醒用户输入。
喜欢的朋友关注我哦!多谢支持!
输入框输入文字后自动出现一键清空键,输入框文字为空时,一键清空键隐藏,下面我们看一下如何通过自定义View实现这一效果。
看一下DeletableEditText.java:
package com.example.testview; 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; /** * @author yayun email:291214603@qq.com blog: http://blog.csdn.net/yayun0516 * @date 2015-08-11 TODO */ public class DeletableEditText extends EditText { private Drawable mRightDrawable; private boolean isHasFocus; public DeletableEditText(Context context) { super(context); init(); } public DeletableEditText(Context context, AttributeSet attrs) { super(context, attrs); init(); } public DeletableEditText(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 start, int before, int count) { } } // 隐藏或显示右边clean的图标 protected void setClearDrawableVisible(boolean isVisible) { Drawable rightDrawable; if (isVisible) { rightDrawable = mRightDrawable; } else { rightDrawable = null; } // 使用代码设置该控件left, top, right, and bottom处的图标 setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], rightDrawable, getCompoundDrawables()[3]); } // 显示动画 public void setShakeAnimation() { this.startAnimation(shakeAnimation(5)); } // CycleTimes动画重复的次数 public Animation shakeAnimation(int CycleTimes) { Animation translateAnimation = new TranslateAnimation(0, 10, 0, 10); translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes)); translateAnimation.setDuration(1000); return translateAnimation; } }
布局文件代码:
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:gravity="center" android:text="登 录" android:textSize="25sp" /> <com.example.testview.DeletableEditText android:id="@+id/det_test" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/title" android:drawableLeft="@drawable/user_account" android:drawableRight="@drawable/user_delete" android:ems="10" android:hint="请输入帐号名" /> <com.example.testview.DeletableEditText android:id="@+id/user_password_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/det_test" android:layout_marginTop="10dp" android:drawableLeft="@drawable/user_password" android:drawableRight="@drawable/user_delete" android:ems="10" android:hint="请输入密码" android:inputType="textPassword" android:singleLine="true" /> </RelativeLayout>
运行实例就可以得到上图的效果,可以实现一键清空。当然里面也写了动画,判断用户是否输入内容,没有输入内容时,可以通过动画提醒用户输入。
喜欢的朋友关注我哦!多谢支持!
相关文章推荐
- android模拟器创建时的PANIC: Could not open:错误的解决
- 菜鸟学Android(四十五):EL表达式和EL函数库
- Android项目使用support v7时遇到的各种问题(转载百度经验)
- 【Android】ViewPager实现无限循环滚动
- AndroidCapturer小而便捷的Android开发截图工具
- Android Studio优化
- android 传感器必知必会 (上)
- android布局篇:Fragment,带侧边栏的activity以及table activity
- Android 编程 权威 指南 阅读 笔记
- Android开发之QQ直接登陆功能
- android学习笔记NO.3
- AnimationDrawable OOM
- android:installLocation="preferExternal"的意义
- Android广播机制
- 《第一行代码Android》阅读笔记
- Android手机进行无线EAP-TLS证书认证配置
- android常用到得单元测试
- Android自定义属性时TypedArray的使用方法
- 教你自己实现一个事件总线EventBus
- MPAndroidChart开源图表库---折线图