Android带清除按钮、密码可见小眼睛的输入框
前言
相信不少小伙伴在开发登录功能时候,或多或少都会用到输入框清除按钮、密码可见与隐藏小眼睛按钮,我之前也是但写这个功能,但是在找回密码、忘记密码里面还要重新写一次很是麻烦,现在我把它只做了一个自定义控件,可以配置清除按钮、密码可见小眼睛是否显示以及图标,有渐隐渐显和横向移动动画,不知道怎么上传可以直接观看的视频,附上三张效果图,可以点击链接观看视频是否是你想要的效果。
一、自定义控件clearedittext
我的这个自带清除、小眼睛的控件,是一个自定义控件,控件名称clearedittext.class,继承relativelayout并有布局引用。在拷贝到项目里面后,使用时候只需要与普通输入框控件一样,进行相关的配置即可,部分属性需要使用自定义属性,具体见代码。
二、使用步骤
1.拷贝clearedittext.class
拷贝clearedittext.class类到自己的项目里,再考拷贝它相关的布局文件、自定义属性、默认图片即可,下面贴出clearedittext主要代码:
public class clearedittext extends relativelayout { private imageview ivlefticon; private edittext myedie; private imageview iveditclean; private imageview ivediteye; private boolean ischecked = true; private context mcontext; private typedarray mtypedarray; private boolean showclean = true;//清空图标是否显示,true:显示 private boolean showeye = false;//密码可见图标是否显示,true:显示 private int drawableleft = -1;//是否显示输入框左侧图片 private int drawableeyeopen = r.drawable.clear_icon_eye_open;//可以看见密码小眼睛样式 private int drawableeyeclose = r.drawable.clear_icon_eye_close;//不可见密码小眼睛样式 private int drawableclean = r.drawable.clear_icon_close;//清除按钮图片 private int cleanpadding = 0;//清除按钮padding边距 private string hintstr; private string textstr; private int mtextcolorhint = color.black; private int mtextcolor = color.black; private int mtextsize = -1; private int mmaxlength = 2000; private int mmaxlines = 1; private int minputtype = 0;//输入类型,就做了不限制、数字、文本密码三种 private boolean isinput = false;//输入1个字符后更改状态为true,保证小眼睛移动一次 private boolean ishideclean = false;//输入字符后,清除了需要小眼睛归为,清除按钮隐藏 private int ivwidth = 45;//关闭按钮的宽度 public clearedittext(context context) { super(context); mcontext = context; initview(); } public clearedittext(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); mcontext = context; mtypedarray = mcontext.obtainstyledattributes(attrs, r.styleable.myedittext); initview(); } public clearedittext(context context, attributeset attrs) { super(context, attrs); mcontext = context; mtypedarray = mcontext.obtainstyledattributes(attrs, r.styleable.myedittext); showclean = mtypedarray.getboolean(r.styleable.myedittext_showclean, showclean); drawableclean = mtypedarray.getresourceid(r.styleable.myedittext_drawableclean, drawableclean); cleanpadding = mtypedarray.getdimensionpixelsize(r.styleable.myedittext_cleanpadding, cleanpadding); showeye = mtypedarray.getboolean(r.styleable.myedittext_showeye, showeye); drawableleft = mtypedarray.getresourceid(r.styleable.myedittext_drawableleft, -1); drawableeyeclose = mtypedarray.getresourceid(r.styleable.myedittext_drawableeyeclose, drawableeyeclose); drawableeyeopen = mtypedarray.getresourceid(r.styleable.myedittext_drawableeyeopen, drawableeyeopen); hintstr = mtypedarray.getstring(r.styleable.myedittext_hint); textstr = mtypedarray.getstring(r.styleable.myedittext_text); mtextcolorhint = mtypedarray.getcolor(r.styleable.myedittext_textcolorhint, mtextcolorhint); mtextcolor = mtypedarray.getcolor(r.styleable.myedittext_textcolor, mtextcolor); mtextsize = mtypedarray.getdimensionpixelsize(r.styleable.myedittext_textsize, mtextsize); mmaxlength = mtypedarray.getinteger(r.styleable.myedittext_maxlength, mmaxlength); mmaxlines = mtypedarray.getdimensionpixelsize(r.styleable.myedittext_maxlines, mmaxlines); minputtype = mtypedarray.getinteger(r.styleable.myedittext_inputtype, minputtype); mtypedarray.recycle(); initview(); } // 初始化视图 private void initview() { view view = view.inflate(getcontext(), r.layout.clear_layout_view, null); ivlefticon = (imageview) view.findviewbyid(r.id.iv_edit_left_icon); myedie = (edittext) view.findviewbyid(r.id.view_edit_show); iveditclean = (imageview) view.findviewbyid(r.id.iv_edit_clean); ivediteye = (imageview) view.findviewbyid(r.id.iv_edit_eye); myedie.sethint(hintstr); myedie.sethinttextcolor(mtextcolorhint); myedie.settext(textstr); myedie.settextcolor(mtextcolor); myedie.setmaxlines(mmaxlines); myedie.setfilters(new inputfilter[]{new inputfilter.lengthfilter(mmaxlength)}); if (mtextsize != -1) { myedie.settextsize(typedvalue.complex_unit_px, mtextsize); } else { myedie.settextsize(15); } if (minputtype == 1) { myedie.setinputtype(inputtype.type_class_number); } else if (minputtype == 2) { myedie.setinputtype(inputtype.type_text_variation_password | inputtype.type_class_text); } else { myedie.setinputtype(inputtype.type_number_variation_normal | inputtype.type_class_text); } if (showeye) { myedie.settransformationmethod(new asteriskpasswordtransformationmethod()); } if (showclean && showeye) { int left = myedie.getpaddingleft(); int top = myedie.getpaddingtop(); int bottom = myedie.getpaddingbottom(); myedie.setpadding(left, top, utils.dp2px(mcontext, 90), bottom); } else if (!showclean && !showeye) { int left = myedie.getpaddingleft(); int top = myedie.getpaddingtop(); int right = myedie.getpaddingright(); int bottom = myedie.getpaddingbottom(); myedie.setpadding(left, top, right, bottom); } else { int left = myedie.getpaddingleft(); int top = myedie.getpaddingtop(); int bottom = myedie.getpaddingbottom(); myedie.setpadding(left, top, utils.dp2px(mcontext, 45), bottom); } myedie.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { if (s.length() > 0) { ishideclean = false; } } @override public void aftertextchanged(editable s) { if (s.length() > 0 && !isinput) {//输入字符大于0且只有一个字符时候显示清除按钮动画,小眼睛移动出位置给清除按钮使用 showeditclean(); moveediteye(); isinput = true; } else if (s.length() == 0) {//无字符小眼睛归位 undoediteye(); } if (s.length() == 0 & !ishideclean) { hideeditclean(); ishideclean = true; isinput = false; } if (oneditinputlistener != null) { oneditinputlistener.input(gettext()); } } }); seteditclean(showclean); iveditclean.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { myedie.settext(""); } }); iveditclean.setimageresource(drawableclean); iveditclean.setpadding(cleanpadding, cleanpadding, cleanpadding, cleanpadding); setediteye(showeye); ivediteye.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (ischecked) { // 输入一个对用户可见的密码 myedie.settransformationmethod(hidereturnstransformationmethod.getinstance()); myedie.setselection(gettext().length()); ivediteye.setimageresource(drawableeyeopen); ischecked = false; } else { // 输入一个对用户不可见的密码 myedie.settransformationmethod(new asteriskpasswordtransformationmethod()); myedie.setselection(gettext().length()); ivediteye.setimageresource(drawableeyeclose); ischecked = true; } } }); if (drawableleft != -1) { ivlefticon.setvisibility(view.visible); ivlefticon.setimageresource(drawableleft); } else { ivlefticon.setvisibility(view.gone); } view.setlayoutparams(new layoutparams(viewpager.layoutparams.match_parent, viewpager.layoutparams.wrap_content)); addview(view); } //密码不可见时候,使用*替换密码 public class asteriskpasswordtransformationmethod extends passwordtransformationmethod { @override public charsequence gettransformation(charsequence source, view view) { return new passwordcharsequence(source); } private class passwordcharsequence implements charsequence { private charsequence msource; public passwordcharsequence(charsequence source) { msource = source; // store char sequence } 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 } } } public string gettext() { return myedie.gettext().tostring().trim(); } public void settext(string text) { myedie.settext(text); } //代码设置是否显示清除按钮 public void seteditclean(boolean iscanclose) { showclean = iscanclose; } //代码设置是否显示小眼睛 public void setediteye(boolean iscansee) { showeye = iscansee; if (showeye == true) { ivediteye.setvisibility(view.visible); } else { ivediteye.setvisibility(view.gone); } } private void showeditclean() { if (showclean == true) { animationutils.showandhiddencenteranimation(iveditclean, animationutils.animationstate.state_show, 500); } } private void hideeditclean() { if (showclean == true) { animationutils.showandhiddencenteranimation(iveditclean, animationutils.animationstate.state_hidden, 500); } } private void moveediteye() { if (showeye) { objectanimator.offloat(ivediteye, "translationx", -utils.dp2px(mcontext, ivwidth)).setduration(500).start(); } } private void undoediteye() { if (showeye) { objectanimator.offloat(ivediteye, "translationx", 0).setduration(500).start(); } } public oneditinputlistener oneditinputlistener; public void setoneditinputlistener(oneditinputlistener listener) { oneditinputlistener = listener; } //输入监听 public interface oneditinputlistener { void input(string content); } }
2.使用示例
下面是在xml布局时候,如何使用clearedittext的示例,部分自定义属性使用xmlns:app="http://schemas.android.com/apk/res-auto"引用调用。app:showeye属性true-显示小眼睛,false-关闭小眼睛。
<com.huaweixia.clear.clearedittext android:id="@+id/et_login_password" android:layout_width="0dp" android:layout_height="45dp" android:layout_margintop="30dp" android:background="@drawable/line_bg_white_only_bottom_d8" android:paddingleft="10dp" app:hint="请输入密码" app:layout_constraintleft_toleftof="@id/et_login_number" app:layout_constraintright_torightof="@id/et_login_number" app:layout_constrainttop_tobottomof="@id/et_login_number" app:showeye="true" app:textcolor="@color/tv_black_333333" app:textcolorhint="@color/tv_gray_999999" app:textsize="15sp" />
总结
一开始我之前自己用的自带清空、密码可见的自定义输入框,是没有清除按钮显示,密码可见按钮移动动效,清除按钮显示比较僵硬,在清除按钮隐藏时候小眼睛按钮后面有一定空白,显示看着比较别扭,加了动效后视觉上感觉更加顺畅,希望对部分需要的小伙伴有空,下面附上 下载demo的链接 地址,有需要可以下载看看。
到此这篇关于android带清除按钮、密码可见小眼睛的输入框的文章就介绍到这了,更多相关android密码输入框内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!