今天分享一下登陆界面用户登录名的检测,大家都知道如果在服务器端进行所有用户名的检测是比较浪费资源的。用户每点击一次登陆就要发到服务端去检测,对于服务端来说负荷是比较大的。所以呢在客服端对用户的非法信息进行简单的过滤是非常有必要的。
源码下载:Android用户名检测
首先看一下效果:
当用户输入的用户名长度小于3,或者大于9时将出现红色提示,并且登陆按钮不可点击。
当输入的用户名大在合法区间则提示消失,如果密码不为空则登陆按钮可点击
虽然功能很小却用到了不少的东西:
接下来看一下源码,为了是登陆界面更加美观,我对登陆控件进行了圆形化处理,也就是开源醒目circleimageVIEw 项目主页地址:https://github.com/hdodenhof/CircleImageView:
<linearLayout 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:orIEntation="vertical" androID:background="@color/colorLogin" > <!-- Login progress --> <Progressbar androID:ID="@+ID/login_progress" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginBottom="8dp" androID:visibility="gone" /> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="180dp" androID:ID="@+ID/head_img" > <de.hdodenhof.circleimagevIEw.circleimageVIEw androID:layout_wIDth="80dp" androID:layout_height="80dp" androID:src="@mipmap/nav_head" androID:layout_alignParentBottom="true" androID:layout_centerHorizontal="true" androID:layout_marginBottom="25dp" /> </relativeLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:paddingBottom="20dp" androID:orIEntation="vertical"> <EditText androID:ID="@+ID/et_user" androID:layout_wIDth="match_parent" androID:layout_height="60dp" androID:hint="@string/username" androID:background="@color/colorLoginForm" androID:layout_marginBottom="5dp" /> <TextVIEw androID:ID="@+ID/tv_tip" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textcolor="@color/error" /> <EditText androID:ID="@+ID/et_pass" androID:layout_wIDth="match_parent" androID:layout_height="60dp" androID:background="@color/colorLoginForm" androID:hint="@string/passWord" androID:paddingtop="1dp" /> </linearLayout> <button androID:ID="@+ID/button" androID:layout_wIDth="match_parent" androID:layout_height="50dp" androID:background="@color/loginbutton" androID:text="@string/loginbutton" androID:textcolor="@color/colorLoginForm" /></linearLayout>
然后修改MainAvtivity.class:
public class MainActivity extends AppCompatActivity { EditText etUser; EditText etPassWord; TextVIEw tvTip; button button; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //初始化VIEw控件 findVIEw(); //用于检测输入的用户名 *** 作 checkLength(); } private voID checkLength() { //为etUser设置焦点改变监听事件 etUser.setonFocuschangelistener(new VIEw.OnFocuschangelistener(){ @OverrIDe public voID onFocusChange(VIEw v,boolean hasFocus) { //如果失去焦点则进行用户名的检测 if(etUser.hasFocus()==false){ //如果用户名长度小于3或者大于9,则提示用户名错误且登陆不可点击 if(etUser.getText().toString().length()>9||etUser.getText().toString().length()<3){ tvTip.setText("用户名不合法!"); button.setClickable(false); }else{ //如果用户名合法且密码不为空,设置提示字体消失按钮可点击 if(etPassWord.getText().toString()!=""){ button.setClickable(true); tvTip.setText(""); } } } } }); } private voID findVIEw() { etUser= (EditText) findVIEwByID(R.ID.et_user); etPassWord= (EditText) findVIEwByID(R.ID.et_pass); tvTip= (TextVIEw) findVIEwByID(R.ID.tv_tip); button= (button) findVIEwByID(R.ID.button); }}
整个代码的核心是编辑框的焦点改变的监听,然后对用户名进行判断。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android登陆界面用户名检测功能全部内容,希望文章能够帮你解决Android登陆界面用户名检测功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)