Android 的简单注册,登录,验证用户,跳转,注销用户
- 效果图
- 注册页面的xml代码
- 注册页面的java代码
- 登录界面的xml代码
- 登录界面的Java代码
- 显示界面的xml代码
- 显示界面的代码
效果图
注册页面的xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="@android:color/white"
tools:context=".MainActivity">
<EditText
android:id="@+id/user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:hint="输入用户名"
android:inputType="textPersonName"
android:textSize="25sp"
android:textStyle="italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
<EditText
android:id="@+id/passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="输入该学号"
android:inputType="numberPassword"
android:textSize="25sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/user" />
<Button
android:id="@+id/log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:background="@android:color/white"
android:text="登 录"
android:textColor="#696969"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwd" />
<Button
android:id="@+id/quit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="33dp"
android:background="@android:color/white"
android:text="退 出"
android:textColor="#696969"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/log"
app:layout_constraintTop_toBottomOf="@+id/passwd" />
<Button
android:id="@+id/logon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="33dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="40dp"
android:background="@android:color/white"
android:text="注 册"
android:textColor="#696969"
android:textSize="20sp"
app:layout_constraintEnd_toStartOf="@+id/log"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwd" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="154dp"
android:layout_height="203dp"
android:layout_marginTop="80dp"
android:foregroundGravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="376dp"
android:layout_height="80dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:foregroundGravity="center"
app:layout_constraintEnd_toStartOf="@+id/right"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo1" />
<ImageView
android:id="@+id/right"
android:layout_width="30dp"
android:layout_height="24dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/right" />
id是ImageView2和imageView3是我设置的两张图片,可以自己选择自己 喜欢的图片,也可以不要;@+id/right这里是实现跳转到信息界面,我用的是张图片,你可以用其他图片或者文字,都可以。
注册页面的java代码
package com.example.finishwork;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText user,passwd;
private Button log,quit,logon;
private String send01,send02;
private ImageView right;
private int right_num = 0; //设置监听事件,看是否登录成功
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
log = findViewById(R.id.log);
quit = findViewById(R.id.quit);
logon = findViewById(R.id.logon);
user = findViewById(R.id.user);
passwd = findViewById(R.id.passwd);
right = findViewById(R.id.right);
//点击注册按钮
logon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//转跳到注册界面Main2Activity
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
//点击登录按钮发生的事件
log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建初始用户
SharedPreferences login = getSharedPreferences("data505",MODE_PRIVATE);
SharedPreferences.Editor editor = login.edit();
editor.putString("Root","000000000000");
editor.commit();
send01 = user.getText().toString();
send02 = passwd.getText().toString();
//用正则表达式以判断用户输入的用户名是否符合规则
String regex = "[a-zA-Z]{4,}";
//对输入的用户名和学号格式进行检验
if (TextUtils.isEmpty(send01)) {
Toast.makeText(MainActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
} else if (!send01.matches(regex)) {
Toast.makeText(MainActivity.this, "请输入规范用户名", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(send02)) {
Toast.makeText(MainActivity.this, "请输入相应学号", Toast.LENGTH_SHORT).show();
} else if (send02.length() != 12) { //这里设置密码长度为12,纯属因为我们学校学号12位
Toast.makeText(MainActivity.this, "请输入正确格式的学号", Toast.LENGTH_SHORT).show();
} else {
judge(); //调用自定义judge()函数来判断是否能成功登录
}
//用户名和学号置空
user.setText("");
passwd.setText("");
}
});
//点击转跳到信息界面的手指
right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(right_num == 1){
Intent intent = new Intent(MainActivity.this,logActivity.class);
startActivity(intent);
}else{
Toast.makeText(MainActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
}
}
});
//退出程序
quit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finishAffinity();
System.exit(0); //杀死进程,方法一
//android.os.Process.killProcess(android.os.Process.myPid()); //杀死进程,方法二
}
});
}
//当输入的用户名和学号格式正确的时候,检测该用户是否注册,密码是否正确
protected void judge() {
SharedPreferences shargetSharedPreferences("data505",MODE_PRIVATE); //data505,是我自己设置的文件名,可以自己更改
//登录界面的用户名和已注册的用户名比较,看该用户是否存在
if(send02.equals(shar.getString(send01,""))){
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
right_num = 1;
//登录成功后将信息传到data505,方便信息显示界面调用
SharedPreferences.Editor userMessage = shar.edit();
userMessage.putString("user_name",send01);
userMessage.putString("user_passwd",send02);
userMessage.commit();
}else {
Toast.makeText(MainActivity.this, "用户名或密码不对", Toast.LENGTH_SHORT).show();
}
}
}
关于杀死进程的方法,我之前直接用的System.exit(0);但是它不能实现,在两个界面一直跳转,这是我百度找到的解决方法,具体出处…有点儿久忘了,你要想找,百度一下会很多的,嘻嘻嘻…哦,还有创建初始用户其实是没甚么必要的,因为当时作业老师要求,才这么设计的,可以删了,直接注册,其实这个初始用户的设计不是怎么合理(自己做的),有什么好的方法可以给提一提,感谢。
登录界面的xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="@android:color/white"
tools:context=".Main2Activity">
<EditText
android:id="@+id/new_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="288dp"
android:hint=" 注册用户名"
android:inputType="textPersonName"
android:textSize="20sp"
android:textStyle="italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/new_passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint=" 注册学号"
android:inputType="numberPassword"
android:textSize="20sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/new_user" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="154dp"
android:layout_height="203dp"
android:layout_marginTop="80dp"
android:foregroundGravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="392dp"
android:layout_height="88dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:foregroundGravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo2" />
<Button
android:id="@+id/logon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginLeft="56dp"
android:layout_marginTop="90dp"
android:background="@android:color/white"
android:text="注 册"
android:textColor="#696969"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/new_passwd" />
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="56dp"
android:layout_marginRight="56dp"
android:background="@android:color/white"
android:text="返回登录"
android:textColor="#C0C0C0"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/new_passwd" />
</androidx.constraintlayout.widget.ConstraintLayout>
这里貌似没啥好说的,继续放代码
登录界面的Java代码
package com.example.finishwork;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
private EditText new_user,new_passwd;
private Button logon1,back;
public String re_send1,re_send2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
new_user = findViewById(R.id.new_user);
new_passwd = findViewById(R.id.new_passwd);
logon1 = findViewById(R.id.logon1);
back = findViewById(R.id.back);
//点击注册按钮发生的事件
logon1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//用正则表达式以判断用户输入的用户名是否符合规则
String regex = "[a-zA-Z]{4,}";
re_send1 = new_user.getText().toString();
re_send2 = new_passwd.getText().toString();
//对注册的用户名和学号格式进行检验
if (TextUtils.isEmpty(re_send1)) {
Toast.makeText(Main2Activity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
} else if (!re_send1.matches(regex)) {
Toast.makeText(Main2Activity.this, "请输入规范的用户名", Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(re_send2)) {
Toast.makeText(Main2Activity.this, "请输入相应学号", Toast.LENGTH_SHORT).show();
} else if (re_send2.length() != 12) {
Toast.makeText(Main2Activity.this, "请设置12位的学号", Toast.LENGTH_SHORT).show();
} else {
judge_user(re_send1); //调用自定义函数来判断是否注册成功
}
}
});
//点击返回登录按钮发生的事件
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//转跳到登录界面MainActivity
Intent i = new Intent(Main2Activity.this,MainActivity.class);
startActivity(i);
//finish();
}
});
}
//注册新用户,将新注册的用户添加到data505
protected void setNew_user (int countNum) {
SharedPreferences login = getSharedPreferences("data505",MODE_PRIVATE);
SharedPreferences.Editor editor = login.edit();
editor.putString(re_send1,re_send2);
++countNum;
editor.putInt("countNum",countNum);
editor.commit();
}
//判断新注册的用户是否存在
protected void judge_user(String set_user) {
SharedPreferences shar = getSharedPreferences("data505",MODE_PRIVATE);
//判断该用户是否注册过
if(re_send2.equals(shar.getString(re_send1,""))){
Toast.makeText(Main2Activity.this,"注册不成功",Toast.LENGTH_SHORT).show();
}else {
//获取计数count,共注册的用户数
int count = shar.getInt("countNum",0);
//调用自定义函数,注册新用户
setNew_user(count);
Toast.makeText(Main2Activity.this,"注册成功\n您是第"+ ++count + "位注册此用户的人",Toast.LENGTH_LONG).show();
}
}
}
显示界面的xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="@android:color/white"
android:outlineSpotShadowColor="@android:color/white">
<ImageView
android:id="@+id/imageView"
android:layout_width="87dp"
android:layout_height="74dp"
android:layout_marginTop="5dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
<TextView
android:id="@+id/userName"
android:layout_width="187dp"
android:layout_height="47dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="10dp"
android:text="用户名"
android:textColor=" #C0C0C0"
android:textSize="25dp"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/userMessage"
android:layout_width="367dp"
android:layout_height="193dp"
android:layout_marginStart="30dp"
android:layout_marginTop="145dp"
android:layout_marginEnd="14dp"
android:text="客户信息"
android:textColor="#696969"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/userName" />
<ImageButton
android:id="@+id/go_back"
android:layout_width="38dp"
android:layout_height="36dp"
android:background="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/btn_dialog" />
<pl.droidsonroids.gif.GifImageView
android:id="@+id/gif_image"
android:layout_width="151dp"
android:layout_height="171dp"
android:layout_margin="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/del_user"
android:layout_width="112dp"
android:layout_height="27dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="10dp"
android:ems="10"
android:inputType="textPersonName"
android:text="注销用户?"
android:textColor="#C0C0C0"
android:textSize="20dp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这里插入了一张动图,插入动图的话,如果之前没有,就在Gradle.Scriots—build.grade(Module:app)----dependencies-----implementation ‘pl.droidsonroids.gif:android-gif-drawable:xxx’
显示界面的代码
package com.example.finishwork;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import pl.droidsonroids.gif.GifImageView;
public class logActivity extends AppCompatActivity {
private TextView userName,userMessage,del;
private ImageButton fan; //返回按钮
private GifImageView gifImageView;
private int del_namber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log);
gifImageView = (GifImageView) findViewById(R.id.gif_image);
gifImageView.setImageResource(R.drawable.waiting);
userName = findViewById(R.id.userName);
userMessage = findViewById(R.id.userMessage);
fan = findViewById(R.id.go_back);
del = findViewById(R.id.del_user);
//获取当前用户信息
SharedPreferences sharedPreferences = getSharedPreferences("data505",MODE_PRIVATE);
String user_name = sharedPreferences.getString("user_name","");
String user_password = sharedPreferences.getString("user_passwd","");
int get_user = sharedPreferences.getInt("countNum",0);
del_namber = sharedPreferences.getInt("del_user",0);
userName.setText(user_name);
//判断是否为管理员用户,设置显示信息
if(user_name.equals("Root")) {
userMessage.setText("亲爱的管理员" + user_name + "您好!\n您的密码为"+user_password + "\n共有"+ get_user + "人注册过用户\n共有" + del_namber + "注销用户");
}else {
userMessage.setText("尊敬的"+user_name+"客户您好!\n您的密码为"+user_password+"请牢记!");
}
//点击返回按钮发生事件
fan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(logActivity.this,MainActivity.class);
startActivity(i);
}
});
//点击删除用户发生时间
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//判断该用户是否为可注销用户
if(user_name.equals("Root")) {
Toast.makeText(logActivity.this,"该用户不允许被注销",Toast.LENGTH_SHORT).show();
}else {
//弹框设置
AlertDialog.Builder builder = new AlertDialog.Builder(logActivity.this);
builder.setMessage(user_name +",您确定要注销用户"+"吗?");
builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(user_name);
editor.putInt("del_user",++del_namber);
editor.commit();
Intent i = new Intent(logActivity.this,MainActivity.class);
startActivity(i);
}
});
builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
显示用户信息界面显示用户名和密码是我们作业的要求,可以自行设计