Android自定义EditText实现淘宝登录功能
作者:tsaopin 发布时间:2023-04-20 01:33:04
本文主要是自定义了EditText,当EditText有文本输入的时候会出现删除图标,点击删除图标实现文本的清空,其次对密码的返回做了处理,用*替代系统提供的.。
首先看效果图:
整体布局UI:
<com.example.zdyedittext.ClearEditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_alignTop="@+id/imageView1"
android:layout_marginLeft="17dp"
android:layout_toRightOf="@+id/imageView1"
android:background="@android:color/white"
android:ems="10"
android:hint="手机号"
android:padding="8dp"
android:singleLine="true" />
<com.example.zdyedittext.ClearEditText
android:id="@+id/et_pass_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:background="@android:color/white"
android:password="true"
android:padding="8dp"
android:singleLine="true" />
自定义EditText类
由于自定义EditText理所当然要集成EditText
public class ClearEditText extends EditText
然后添加构造方法,是为了能在XML中能够引用。
public ClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
}
接下来就是设置自己的EditText的样式,添加自己想要的样式。具体是在init()方法中实现。
public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
init()方法的实现过程:[2]参数为:dr.mDrawableRight,定义删除按钮是在EditText的右边,设置图标的左上右下:mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
private void init() {
// 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
mClearDrawable = getCompoundDrawables()[2];
if (mClearDrawable == null) {
mClearDrawable = getResources().getDrawable(R.drawable.del);//R.drawable.del删除图标的图片
}
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
//设置图标的左上右下
// 默认设置隐藏图标
setClearIconVisible(false);
// 设置焦点改变的监听
setOnFocusChangeListener(this);
// 设置输入框里面内容发生改变的监听
addTextChangedListener(this);
}
由于不能直接给EditText设置监听事件,所以采用记录点击位置来模拟点击事件,只记录了鱼图标的左右点击。
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);
}
判断输入框中是否有文字,动态设置删除图标的显示和隐藏。
public void onFocusChange(View v, boolean hasFocus) {
this.hasFoucs = hasFocus;
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}
}
如果输入框中有文字 那么久绘制删除图标
protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
当输入框内容发生变化的时候动态改变删除图标
public void onTextChanged(CharSequence s, int start, int count, int after) {
if (hasFoucs) {
setClearIconVisible(s.length() > 0);
}
}
至此就完成了:当属框中没有文本的时候 删除图标隐藏 当有文本输入的时候,删除图标显示,点击删除图标,清空文本内容。
自定义InputType返回为”*”
设置密码样式要继承PasswordTransformationMethod这个类然后实现CharSequence方法去修改CharAt的返回值为“*”即可。
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
这里用于修改InputType的返回样式
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
然后在主程序中初始化控件,在布局中设置android:password=”true”这一行代码,以便在代码中动态设置密码输入的返回样式。
et_pass_word = (ClearEditText) findViewById(R.id.et_pass_word);
et_pass_word.setTransformationMethod(new EditTextBgToStar());
总结:
在自定义的EditText中加入删除图标的监听,由于不能直接设置,所以采用记录按下的位置来模拟点击事件。总体实现思路就是在EditText的右边画一个删除图标,然后动态设置显示或隐藏,通过设置监听事件,来动态显示,清除文本框中的文本。在自定义密码返回样式的时候,主要就是继承PasswordTransformationMethod这个类,实现CharSequence方法,替换输入样式为自定义。
点击下载源码
来源:http://blog.csdn.net/tsaopin/article/details/47266891
猜你喜欢
- 实例如下:一 json optString 解析的TimesTamp string二 long dateSec = (long) (Doub
- 多网卡环境下Eureka服务注册IP选择问题场景服务器上分别配置了eth0和eth1两块网卡,只有eth1的地址可供其它机器访问,在这种情况
- 背景银行跨行转账业务是一个典型分布式事务场景,假设 A 需要跨行转账给 B,那么就涉及两个银行的数据,无法通过一个数据库的本地事务保证转账的
- 平时开发,基本不改变的常量我们都放在了配置项里,如properties或yml文件里,这个时候为了只在启动时候进行加载。如何做呢?我们通过s
- 如果我们只是需要让用户能够拍摄照片,则可以直接请求已有相机应用拍摄照片并将照片返回给我们1、拍照1.1 请求相机功能在清单文件中添加: &n
- 找到framework/base/core/res/res/values/dimens.xml,在其中把Navigation的配置改成0 &
- #region 监视文件夹的变化
- 在两个Activity跳转时,由于第二个Activity在启动时加载了较多数据,就会在启动之前出现一个短暂的黑屏时间,解决这个
- 本文以新建的CUDA的.cu程序来进行说明,同样也适用于C程序。一,发现问题1,首先我们在vs2019中创建了工程以后(我所创建的工程名称为
- 1、背景最近用到了Spring Cloud Alibaba开发微服务,在开发的过程中发现,当我们的服务上线或下线的时候,我们的Spring
- 本文实例为大家分享了Android实现图片加载进度提示的具体代码,供大家参考,具体内容如下先上图:实现原理:第一个控件的实现原理是重写Ima
- Java String类的concat方法在了解concat()之前,首先需要明确的是String的两点特殊性长度不可变值不可变这两点从源码
- 最近在公司,项目不是很忙了,偶尔看见一个兄台在CSDN求助,帮忙要一个自定义的渐变色进度条,我当时看了一下进度条,感觉挺漂亮的,就尝试的去自
- 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来
- 1. 使用蓝牙的响应权限<uses-permission android:name="android.permission.
- 1. 实现原理将key映射到 2^32 - 1 的空间中,将这个数字的首尾相连,形成一个环计算节点(使用节点名称、编号、IP地址)的hash
- 创建AlertDialog的步骤:1、创建AlertDialog.Builder对象2、调用Builder对象的setTitle
- indexof方法:注解:indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1
- 1. selectKey标签查询DDLCREATE TABLE `luck_reward_info` ( `id` int NO
- 资源下载:点此下载一、语言和环境1.实现语言: JAVA语言。2.环境要求: MyEclipse/Eclipse + Tomcat + My