温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

制作Android登录页面的方法

发布时间:2020-08-15 09:43:23 来源:亿速云 阅读:179 作者:小新 栏目:开发技术

这篇文章主要介绍制作Android登录页面的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

效果演示

制作Android登录页面的方法
制作Android登录页面的方法

密码显示与隐藏

方法一

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登录页面的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI