本篇文章主要是制作登录界面与注册界面,并使用Android中用于存储轻量级数据的SharedPreferences存储用户输入的数据
这里就不介绍SharedPreferences的使用了,不懂的可以搜索SharedPreferences的使用
登录布局文件 login_activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
>
<!--<ImageView
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="200dp"
android:src="@drawable/bg01"
/>-->
<TextView
android:layout_margin="20dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
android:textColor="#FF9800"
android:text="登 录 界 面"
/>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<!-- 账号 -->
<LinearLayout
android:background="@drawable/login_et_bg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
>
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_account_box_24"/>
<EditText
android:id="@+id/et_acount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:background="@null"
android:textSize="14sp"
android:hint="请输入用户名"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="10dp"/>
<!-- 密码 -->
<LinearLayout
android:background="@drawable/login_et_bg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
>
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_lock_24"/>
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:background="@null"
android:textSize="14sp"
android:inputType="textPassword"
android:hint="请输入密码"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="10dp"/>
<TextView
android:id="@+id/tv_register"
android:layout_margin="10dp"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF9800"
android:text="还未注册?"
/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/login_btn"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/white"
android:text="登 录"
/>
</LinearLayout>
</LinearLayout>
图标(登陆注册界面都是这两个图标)
baseline_account_box_24.xml
<vector android:height="24dp" android:tint="#EB9656"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M3,5v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2L5,3c-1.11,0 -2,0.9 -2,2zM15,9c0,1.66 -1.34,3 -3,3s-3,-1.34 -3,-3 1.34,-3 3,-3 3,1.34 3,3zM6,17c0,-2 4,-3.1 6,-3.1s6,1.1 6,3.1v1L6,18v-1z"/>
</vector>
baseline_lock_24.xml
<vector android:height="24dp" android:tint="#EB9656"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM12,17c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2zM15.1,8L8.9,8L8.9,6c0,-1.71 1.39,-3.1 3.1,-3.1 1.71,0 3.1,1.39 3.1,3.1v2z"/>
</vector>
LinearLayout样式login_et_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 背景颜色-->
<solid android:color="@color/white" />
<!-- 圆角(上下左右)-->
<corners android:radius="25dp" />
<!-- 边框颜色和宽度-->
<stroke
android:width="1dp"
android:color="#f5f5f5" />
</shape>
登录代码 LoginActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_acount;
private EditText et_pwd;
private SharedPreferences sp;
private Button btn_login;
private TextView tv_register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
ViewClick();
}
/**
* 初始化控件
*/
public void initView(){
et_acount = findViewById(R.id.et_acount);
et_pwd = findViewById(R.id.et_pwd);
btn_login = findViewById(R.id.btn_login);
tv_register = findViewById(R.id.tv_register);
sp = getSharedPreferences("userinfo", MODE_PRIVATE);
}
/**
* 设置控件的点击事件
*/
public void ViewClick(){
btn_login.setOnClickListener(this);
tv_register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_login){
// 获取用户输入的数据
String etAcount = et_acount.getText().toString();
String etPwd = et_pwd.getText().toString();
// 点击登录,获取SharedPreferences里存储的数据(即注册里创建的共享文件)
String acount = sp.getString("acount", "");
String pwd = sp.getString("pwd", "");
// 判断用户输入的数据与SharedPreferences里存储的数据是否一致
if(acount.equals(etAcount) && pwd.equals(etPwd)){
// 满足条件,登录成功
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}else {
// 不满足条件
Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
} else if (v.getId() == R.id.tv_register) {
// 跳转注册页面
startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
// 清空输入框
et_pwd.setText("");
et_acount.setText("");
}
}
}
登录页面预览
注册界面布局文件 register_activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:src="@drawable/left" />
</RelativeLayout>
<TextView
android:layout_margin="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
android:textColor="#FF9800"
android:text="注 册 界 面"
/>
<LinearLayout
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<!-- 账号 -->
<LinearLayout
android:background="@drawable/login_et_bg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
>
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_account_box_24"/>
<EditText
android:id="@+id/et_acount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:background="@null"
android:textSize="14sp"
android:hint="请输入用户名"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="10dp"/>
<!-- 密码 -->
<LinearLayout
android:background="@drawable/login_et_bg"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
>
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_lock_24"/>
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:background="@null"
android:textSize="14sp"
android:inputType="textPassword"
android:hint="请输入密码"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="10dp"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/login_btn"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/white"
android:text="注 册"
/>
</LinearLayout>
</LinearLayout>
注册代码 RegisterActivity.java
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.TextView;
import android.widget.Toast;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_acount;
private EditText et_pwd;
private SharedPreferences sp;
private Button btn_register;
private ImageView img_left;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
setSharedPreferences();
initView();
ViewClick();
}
/**
* 创建数据存储文件
*/
public void setSharedPreferences() {
sp = getSharedPreferences("userinfo", MODE_PRIVATE);
}
/**
* 初始化控件
*/
public void initView() {
et_acount = findViewById(R.id.et_acount);
et_pwd = findViewById(R.id.et_pwd);
btn_register = findViewById(R.id.btn_register);
img_left = findViewById(R.id.img_left);
}
/**
* 设置控件的点击事件
*/
public void ViewClick() {
btn_register.setOnClickListener(this);
img_left.setOnClickListener(this);
}
/**
* 校验密码
*/
public boolean verityPassword(String pwd) {
boolean matches = pwd.matches("[1-9]\\d{5,19}");
if (!matches) {
Toast.makeText(RegisterActivity.this, "密码长度应在6~20之间", Toast.LENGTH_SHORT).show();
return false;
} else {
return true;
}
}
public void onClick(View v) {
if (v.getId() == R.id.btn_register) {
String acount = et_acount.getText().toString();
String pwd = et_pwd.getText().toString();
// 判断账号和密码是否为空
if (!TextUtils.isEmpty(acount) && !TextUtils.isEmpty(pwd)) {
// 调用检验密码函数,进行密码的校验
boolean flag = verityPassword(pwd);
if (flag) {
Toast.makeText(RegisterActivity.this, "注册成功,请返回登录!", Toast.LENGTH_SHORT).show();
// 将账号和密码存起来
sp.edit().putString("acount",acount).apply();
sp.edit().putString("pwd",pwd).apply();
}
}else{
Toast.makeText(RegisterActivity.this, "账号和密码不能为空", Toast.LENGTH_SHORT).show();
}
} else if (v.getId() == R.id.img_left) {
// 结束活动
finish();
}
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
super.onPointerCaptureChanged(hasCapture);
}
}
注册页面预览
至此整个简单的登录、注册制作与使用SharedPreferences进行数据的存储就完成了,还有很多地方可优化,美中不足的地方,各位大佬可以在评论区探讨探讨,给一点点建议,希望这个小小的分享能帮到你们,带给你灵感,我们共同进步。