大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
我这里主要讲解的是使用xutils进行开发的,所以在开发之前一定要先导入xutils项目包。
下载链接:https://download.csdn.net/download/weixin_42497748/10754925
1.在AndroidManifest.xml里面开通权限
<!--网络权限-->
<uses-permission android:name="android.permission.INTERNET" />
<!--外部存储权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2.创建register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="@+id/zc_nic"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:hint="请输入昵称"
android:textSize="16sp"
android:background="@null"
android:inputType="number"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="@+id/zc_pwd"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:hint="请输入密码"
android:textSize="16sp"
android:background="@null"
android:inputType="number"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="@+id/zc_repwd"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:hint="请再次输入密码"
android:textSize="16sp"
android:background="@null"
android:inputType="number"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="@+id/zc_model"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:hint="请输入您的手机号"
android:textSize="16sp"
android:background="@null"
android:inputType="number"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="@+id/zc_code"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="4"
android:background="@null"
android:layout_marginLeft="10dp"
android:hint="请输入验证码"
android:textSize="16sp"
android:inputType="number"
/>
<Button
android:id="@+id/btn_getcode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.9"
android:textSize="13sp"
android:gravity="center"
android:text="获取验证码"
android:layout_gravity="center"
android:background="#009B63"
/>
</LinearLayout>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="注 册"
android:textSize="18sp"
android:background="#009B63"
/>
</LinearLayout>
3.创建RegisterActivity.java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import xiangyue.zy.lyg.xiangyue.R;
import xiangyue.zy.lyg.xiangyue.config.URLConfig;
import xiangyue.zy.lyg.xiangyue.dao.UserDao;
import xiangyue.zy.lyg.xiangyue.utils.T;
@ContentView(R.layout.register)
public class RegisterActivity extends Activity implements View.OnClickListener{
private int countSeconds=60;//倒计时
private Context mContext;
private String usersuccess;
/**
* 手机号
*/
@ViewInject(R.id.zc_model)
private EditText zc_model;
/**
* 验证码
*/
@ViewInject(R.id.zc_code)
private EditText zc_code;
/**
* 获取验证码
*/
@ViewInject(R.id.btn_getcode)
private Button btn_getcode;
/**
* 注册
*/
@ViewInject(R.id.btn_register)
private Button btn_register;
/**
* 昵称
*/
@ViewInject(R.id.zc_nic)
private EditText zc_nic;
/**
* 密码
*/
@ViewInject(R.id.zc_pwd)
private EditText zc_pwd;
/**
* 昵称
*/
@ViewInject(R.id.zc_repwd)
private EditText zc_repwd;
//用户数据库操作对象
private UserDao userDao = new UserDao();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
x.view().inject(this);
initView();
initEvent();
initData();
}
/**
* 注册验证
*/
@Event(R.id.btn_register)
private void btnLoginClick(View v) {
//判断用户输入是否符合标准
if (TextUtils.isEmpty(this.zc_nic.getText())) {
T.showLong(this,"请填写昵称");
return;
}else if(this.zc_pwd.getText().length()<6 || this.zc_pwd.getText().length()>18){
T.showLong(this,"密码长度为6-18位数");
return;
}else if(!this.zc_repwd.getText().equals(this.zc_pwd.getText())){
T.showLong(this,"两次密码输入不一致");
return;
}else if(TextUtils.isEmpty(this.zc_model.getText())){
T.showLong(this,"手机号不能为空");
}else if(this.zc_model.getText().length()!=11){
T.showLong(this,"请输入正确的手机号");
}else if(this.zc_code.getText().length()!=6){
T.showLong(this,"验证码格式不正确,必须为6位数。");
}
}
/**
* 点击获取验证码
*/
private Handler mCountHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (countSeconds > 0) {
--countSeconds;
btn_getcode.setText("(" + countSeconds + ")后获取验证码");
mCountHandler.sendEmptyMessageDelayed(0, 1000);
} else {
countSeconds = 60;
btn_getcode.setText("请重新获取验证码");
}
}
};
/**
* 初始化文本框
*/
private void initView() {
zc_model = (EditText) findViewById(R.id.zc_model);
btn_getcode = (Button) findViewById(R.id.btn_getcode);
zc_code = (EditText) findViewById(R.id.zc_code);
btn_register = (Button) findViewById(R.id.btn_register);
zc_pwd= (EditText) findViewById(R.id.zc_pwd);
zc_repwd= (EditText) findViewById(R.id.zc_repwd);
zc_nic= (EditText) findViewById(R.id.zc_nic);
}
/**
* 初始化按钮时间
*/
private void initEvent() {
btn_getcode.setOnClickListener(this);
btn_register.setOnClickListener(this);
}
/**
* 初始化日期
*/
private void initData() {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_getcode:
if (countSeconds == 60) {
String mobile = zc_model.getText().toString();
Log.e("tag", "mobile==" + mobile);
getMobiile(mobile);
} else {
Toast.makeText(RegisterActivity.this, "不能重复发送验证码", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_register:
login();
break;
default:
break;
}
}
//获取信息进行登录
public void login() {
String mobile = zc_model.getText().toString().trim();
String verifyCode = zc_code.getText().toString().trim();
RequestParams params = new RequestParams(“你的请求地址”);
x.http().post(params, new Callback.ProgressCallback<String>() {
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
}
@Override
public void onSuccess(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
Log.e("tag", "登陆的result=" + jsonObject);
String success = jsonObject.optString("success");
String data = jsonObject.optString("data");
String msg=jsonObject.optString("msg");
if ("true".equals(success)) {
Log.e("tag","登陆的data="+data);
JSONObject json = new JSONObject(data);
String token = json.optString("token");
String userId = json.optString("userId");
}else{
Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
//获取验证码信息,判断是否有手机号码
private void getMobiile(String mobile) {
if ("".equals(mobile)) {
Log.e("tag", "mobile=" + mobile);
new AlertDialog.Builder(mContext).setTitle("提示").setMessage("手机号码不能为空").setCancelable(true).show();
} else if (isMobileNO(mobile) == false) {
new AlertDialog.Builder(mContext).setTitle("提示").setMessage("请输入正确的手机号码").setCancelable(true).show();
} else {
Log.e("tag", "输入了正确的手机号");
requestVerifyCode(mobile);
}
}
//获取验证码信息,进行验证码请求
private void requestVerifyCode(String mobile) {
RequestParams requestParams = new RequestParams(“你的短信接口地址”);
x.http().post(requestParams, new Callback.ProgressCallback<String>() {
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
}
@Override
public void onSuccess(String result) {
try {
JSONObject jsonObject2 = new JSONObject(result);
Log.e("tag", "jsonObject2" + jsonObject2);
String state = jsonObject2.getString("success");
String verifyCode = jsonObject2.getString("msg");
Log.e("tag", "获取验证码==" + verifyCode);
if ("true".equals(state)) {
Toast.makeText(RegisterActivity.this, verifyCode, Toast.LENGTH_SHORT).show();
startCountBack();//这里是用来进行请求参数的
} else {
Toast.makeText(RegisterActivity.this, verifyCode, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
ex.printStackTrace();
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
//使用正则表达式判断电话号码
public static boolean isMobileNO(String tel) {
Pattern p = Pattern.compile("^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$");
Matcher m = p.matcher(tel);
System.out.println(m.matches() + "---");
return m.matches();
}
//获取验证码信息,进行计时操作
private void startCountBack() {
((Activity) mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
btn_getcode.setText(countSeconds + "");
mCountHandler.sendEmptyMessage(0);
}
});
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/160009.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...