Android登陆界面用户名检测功能

 更新时间:2017年03月03日 14:45:30   作者:sjw_night  
这篇文章主要为大家详细介绍了Android登陆界面用户名检测功能的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

今天分享一下登陆界面用户登录名的检测,大家都知道如果在服务器端进行所有用户名的检测是比较浪费资源的。用户每点击一次登陆就要发到服务端去检测,对于服务端来说负荷是比较大的。所以呢在客服端对用户的非法信息进行简单的过滤是非常有必要的。

源码下载:Android用户名检测

首先看一下效果:

这里写图片描述 

当用户输入的用户名长度小于3,或者大于9时将出现红色提示,并且登陆按钮不可点击。

这里写图片描述

当输入的用户名大在合法区间则提示消失,如果密码不为空则登陆按钮可点击
虽然功能很小却用到了不少的东西:

  • EditText失去焦点事件的监听
  • 获取输入的字符并且检测长度
  • 当用户名不合法时出现提示
  • 设置登录按钮的不可点击

接下来看一下源码,为了是登陆界面更加美观,我对登陆控件进行了圆形化处理,也就是开源醒目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"
 style="?android:attr/progressBarStyleLarge"
 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中SQLite 使用方法详解

    Android中SQLite 使用方法详解

    这篇文章主要介绍了Android中SQLite 使用方法详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • Android实战教程第五篇之一键锁屏应用

    Android实战教程第五篇之一键锁屏应用

    这篇文章主要为大家详细介绍了Android实战教程第五篇之一键锁屏应用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android实现二级购物车的全选加反选、总价功能

    Android实现二级购物车的全选加反选、总价功能

    这篇文章主要为大家详细介绍了Android实现二级购物车的全选加反选、总价功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • 深入探究Android动态代理的原理及用途

    深入探究Android动态代理的原理及用途

    动态代理是一种在编程中非常有用的设计模式,它允许你在运行时创建一个代理对象来代替原始对象,以便在方法调用前后执行额外的逻辑,本文将深入探讨Android动态代理的原理、用途和实际示例
    2023-09-09
  • 详解Android中Service AIDL的使用

    详解Android中Service AIDL的使用

    作为一名Android开发人员,如果没听过Service,那就有点说不过去了啊,Service是Android四大组件之一,它是不依赖于用户界面的,就是因为Service不依赖与用户界面,所以我们常常用于进行一些耗时的操作,比如:下载数据等;本文将详细介绍Android中Service AIDL的使用。
    2021-06-06
  • Android 桌面Widget开发要点解析(时间日期Widget)

    Android 桌面Widget开发要点解析(时间日期Widget)

    总的来说,widget主要功能就是显示一些信息。我们今天编写一个很简单的作为widget,显示时间、日期、星期几等信息。需要显示时间信息,那就需要实时更新,一秒或者一分钟更新一次
    2013-07-07
  • Android短信接收监听、自动回复短信操作例子

    Android短信接收监听、自动回复短信操作例子

    本文实现了短信接收监听,当接收到短信时,可自动回复短信,或自动回拨电话,同时监听短信的发送状态
    2014-04-04
  • 详解Android v1、v2、v3签名(小结)

    详解Android v1、v2、v3签名(小结)

    这篇文章主要介绍了详解Android v1、v2、v3签名(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Android运用BroadcastReceiver实现强制下线

    Android运用BroadcastReceiver实现强制下线

    本篇文章主要介绍了Android运用BroadcastReceiver实现强制下线,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android Fragment的静态注册和动态注册创建步骤

    Android Fragment的静态注册和动态注册创建步骤

    这篇文章主要介绍了Android Fragment的静态注册和动态注册创建步骤,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02

最新评论