当前位置: 移动技术网 > IT编程>移动开发>Android > Android 登录页面的实现代码(密码显示隐藏、EditText 图标切换、限制输入长度)

Android 登录页面的实现代码(密码显示隐藏、EditText 图标切换、限制输入长度)

2020年08月17日  | 移动技术网IT编程  | 我要评论
效果演示密码显示与隐藏方法一if(status){ etpassword.setinputtype(inputtype.type_class_text | editorinfo.type_text_v

效果演示

密码隐藏显示、图标切换演示
最长输入限制演示

密码显示与隐藏

方法一

if(status){
 etpassword.setinputtype(inputtype.type_class_text | editorinfo.type_text_variation_normal);		//显示文本
 status = false;
}else {
 etpassword.setinputtype(inputtype.type_class_text | editorinfo.type_text_variation_password);	//隐藏文本
 status = true;
}
etpassword.setselection(etpassword.gettext().tostring().length());	//光标调整到文本末端

方法二

if (status) {
 etpassword.settransformationmethod(hidereturnstransformationmethod.getinstance());	//显示文本
 status = false;
} else {
 etpassword.settransformationmethod(passwordtransformationmethod.getinstance());		//隐藏文本
 status = true;
}

edittext 图标切换

实现方法

//编辑框点击事件,取 icon 点击位置设置点击事件
etpassword.setontouchlistener(new view.ontouchlistener() {
		@override
		public boolean ontouch(view v, motionevent event) {
			// 长度为4的数组,分别表示左、右、上、下四个 icon
			drawable drawable = etpassword.getcompounddrawables()[2];
			if (drawable == null) //如果右边没有图片,不再处理
				return false;
			if (event.getaction() != motionevent.action_up)	//如果不是按下事件,不再处理
				return false;
			if (event.getx() > etpassword.getwidth() - etpassword.getpaddingright() - drawable.getintrinsicwidth()) {	
			//点击范围为右侧 icon 位置
				if (status) {
					status= false;
					//获取小眼睛图标
					drawable icondrawable = getresources().getdrawable(r.drawable.icon_eye_open);
					//设置新图标,分别对应左、上、右、下4个图标
					etpassword.setcompounddrawableswithintrinsicbounds(null, null, icondrawable, null);
				} else {
					status= true;
					drawable icondrawable = getresources().getdrawable(r.drawable.icon_eye_close);
					etpassword.setcompounddrawableswithintrinsicbounds(null, null, icondrawable, null);
				}
			}
			return false;
		}
	});

限制输入长度

方法一:以判断方式控制最大输入长度

private static final int max_input_length = 50;		//限制最大输入长度50

etpassword.setfilters(new inputfilter[]{new inputfilter() {		//通过过滤器进行限制
 @override
 public charsequence filter(charsequence charsequence, int start, int end, spanned spanned, int dstart, int dend) {
  //charsequence 为输入内容(删除时为空),spanned 为输入前输入框内容
  if ((!charsequence.tostring().equals("")) && spanned.tostring().length() >= max_input_length) {
   //判断当前有内容输入(不为删除),且当前内容长度为最大长度,进行 toast 提醒,且返回空
   toast.maketext(myapplication.context, "最大输入长度为50", toast.length_short).show();
   return "";		//返回值为输入框增加内容,返回空不增加,默认返回 null
  }
  return null;
 }
}});

方法二:以过滤器方式控制最大输入长度

etchange.setfilters(new inputfilter[]{new inputfilter() {
 @override
 public charsequence filter(charsequence charsequence, int start, int end, spanned spanned, int dstart, int dend) {
  if((!source.tostring().equals("")) && dest.tostring().length() >= max_input_length){
   toast.maketext(mainactivity.this, "最大输入长度为50", toast.length_short).show();
  }
  return null;
 }
},new inputfilter.lengthfilter(max_input_length)});		//以过滤器方式控制最大输入长度

总结

到此这篇关于android 登录页面的实现代码(密码显示隐藏、edittext 图标切换、限制输入长度)的文章就介绍到这了,更多相关android 登录页面内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网