您的位置:首页 > 移动开发 > Android开发

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐