Android——XUtils3实现登录和注册页面校验,正则验证
2018-01-18 15:23
706 查看
依赖
compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.bigkoo:convenientbanner:2.0.5' compile 'org.xutils:xutils:3.5.0' compile 'com.google.code.gson:gson:2.8.2'
项目清单文件(初始化Utils)
<application android:name=".MyApplication"
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.asus.login4.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="32dp" android:gravity="center" android:textSize="18dp" android:text="登录"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:orientation="vertical" android:id="@+id/shuru"> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/login_phone" android:hint="请输入手机号" android:layout_gravity="center" android:gravity="center" android:layout_marginTop="15dp" /> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/login_pwd" android:gravity="center" android:layout_gravity="center" android:hint="请输入密码" android:layout_marginTop="13dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_below="@id/shuru" android:gravity="center_horizontal" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/login" android:text="登录"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/login_rejister" android:text="注册"/> </LinearLayout> </RelativeLayout>
activity_rejist.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.asus.login4.Rejist"> <TextView android:layout_width="match_parent" android:layout_height="32dp" android:gravity="center" android:textSize="18dp" android:text="注册"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:orientation="vertical" android:id="@+id/shuru"> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/rejist_phone" android:hint="请输入手机号" android:layout_gravity="center" android:gravity="center" android:layout_marginTop="15dp" /> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/rejist_pwd" android:gravity="center" android:layout_gravity="center" android:hint="请输入密码" android:layout_marginTop="13dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_below="@id/shuru" android:gravity=" 10c8d center_horizontal" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rejist_rejister" android:text="注册"/> </LinearLayout> </RelativeLayout>
activity_shou_ye.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.asus.login4.ShouYe"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陆成功!!!"/> </android.support.constraint.ConstraintLayout>
MyApplication
package com.example.asus.login4; import android.app.Application; import org.xutils.x; /** * Created by ASUS on 2018/1/18. */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能. } }
MainActivity
package com.example.asus.login4; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.asus.login4.Bean.LoginBean; import com.example.asus.login4.Bean.RegistBean; import com.google.gson.Gson; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity{ private Button btn_login; private Button btn_regist; private EditText et_mobile; private EditText et_password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //查找控件 btn_login = (Button) findViewById(R.id.login); btn_regist = (Button) findViewById(R.id.login_rejister); et_mobile = (EditText) findViewById(R.id.login_phone); et_password = (EditText) findViewById(R.id.login_pwd); btn_regist.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,Rejist.class); startActivity(intent); } }); btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String mobile = et_mobile.getText().toString().trim(); String password = et_password.getText().toString().trim(); //判断输入的内容是否为phone boolean b = isPhoneNumber(mobile); if (mobile.isEmpty() || password.isEmpty()) { Toast.makeText(MainActivity.this, "用户名/密码不能为空", Toast.LENGTH_SHORT).show(); } else if (!b) { Toast.makeText(MainActivity.this, "手机号不合法", Toast.LENGTH_SHORT).show(); } else if (password.length() < 6) { Toast.makeText(MainActivity.this, "密码不能少于六位数", Toast.LENGTH_SHORT).show(); } else { login(mobile, password); } } }); } private boolean isPhoneNumber(String phoneStr) { //定义电话格式的正则表达式 String regex = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"; //设定查看模式 Pattern p = Pattern.compile(regex); //判断Str是否匹配,返回匹配结果 Matcher m = p.matcher(phoneStr); return m.find(); } private void login(String mobile,String password){ RequestParams params = new RequestParams("http://120.27.23.105/user/login"); params.addQueryStringParameter("mobile",mobile); params.addQueryStringParameter("password",password); x.http().get(params, new Callback.CacheCallback<String>() { @Override public void onSuccess(String result) { //成功 Gson gson = new Gson(); LoginBean loginBean = gson.fromJson(result, LoginBean.class); Toast.makeText(MainActivity.this,loginBean.getMsg(), Toast.LENGTH_SHORT).show(); if (loginBean.getCode().equals("0")){ Intent intent = new Intent(MainActivity.this, ShouYe.class); startActivity(intent); } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } @Override public boolean onCache(String result) { return false; } }); } }
Rejist
package com.example.asus.login4; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.asus.login4.Bean.RegistBean; import com.google.gson.Gson; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; public class Rejist extends AppCompatActivity implements View.OnClickListener { private EditText rejist_phone; private EditText rejist_pwd; private Button rejist_rejister; public Rejist() { } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rejist); init(); setonclick(); } private void setonclick() { rejist_rejister.setOnClickListener(this); } private void init() { rejist_phone = (EditText) findViewById(R.id.rejist_phone); rejist_pwd = (EditText) findViewById(R.id.rejist_pwd); rejist_rejister = (Button) findViewById(R.id.rejist_rejister); } @Override public void onClick(View view) { String mobile = rejist_phone.getText().toString().trim(); String password = rejist_pwd.getText().toString().trim(); switch (view.getId()){ case R.id.rejist_rejister: boolean flag = checkData(mobile, password); //Log.d("zzz", "checkData: "+flag); if(flag){ register(mobile,password); } break; } } private void register(String mobile, String password) { RequestParams params = new RequestParams("http://120.27.23.105/user/reg"); //Log.d("zzz", "params: "+params); params.addQueryStringParameter("mobile",mobile); params.addQueryStringParameter("password",password); x.http().get(params, new Callback.CacheCallback<String>() { @Override public void onSuccess(String result) { Gson gson = new Gson(); RegistBean registBean = gson.fromJson(result, RegistBean.class); Toast.makeText(Rejist.this,registBean.getMsg(),Toast.LENGTH_SHORT).show(); //Log.d("zzz", "Msg: "+registBean.getMsg()); if(registBean.getCode().equals("0")){ finish(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_SHORT).show(); } @Override public void onCancelled(CancelledException cex) { Toast.makeText(x.app(), "cancelled", Toast.LENGTH_SHORT).show(); } @Override public void onFinished() { } @Override public boolean onCache(String result) { return false; } }); } private boolean checkData(String mobile, String password) { if(TextUtils.isEmpty(mobile)||TextUtils.isEmpty(password)){ Toast.makeText(this,"不能为空",Toast.LENGTH_SHORT).show(); return false; } if(password.length()<6){ Toast.makeText(this,"密码长度不能小于6",Toast.LENGTH_SHORT).show(); return false; } return true; } }
ShouYe
package com.example.asus.login4; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class ShouYe extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shou_ye); } }
相关文章推荐
- Android——XUtils3实现登录和注册页面校验
- Android项目开发(3)-注册验证码验证页面功能实现
- Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能
- 手写注册页面,并实现验证信息(AJAX,Jquery,正则表达式,密码强度验证)
- 实现正则验证手机的登录与注册
- Android基础学习总结(十二)——利用Bmob实现用户账户体系(注册、登录、验证等)
- Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能
- Android登录注册页面实现分享
- Java使用正则表达式对注册页面进行验证功能实现
- ASP.NET jQuery 食谱11 (通过使用jQuery validation插件简单实现用户登录页面验证功能)
- php自动注册登录验证机制实现代码
- 1 完成登录页面(到记事本文件进行密码和用户名的验证)。 2 完成 用户注册页面(将注册信息显示在本页面 div 中)。
- 实现session登陆时间的验证,验证用户登录页面的一个监听器
- 实现用户注册功能:用servlet验证提交的数据是否合法,不合法返回注册页面,并提示出错。
- 在登录页面中js进行正则验证电话号码和邮箱地址,并使用ajax进行用户ID的数据库验证
- Android基于XMPP Smack Openfire下学习开发IM(一)实现用户注册、登录、修改密码和注销等
- Android基于XMPP Smack Openfire下学习开发IM(一)实现用户注册、登录、修改密码和注销等
- Lance老师UI系列教程第三课->QQ登录注册界面的实现(android-2012最新版)
- 单java+2jsp页面实现登录验证
- Lance老师UI系列教程第三课->QQ登录注册界面的实现(android-2012最新版)