1. 项目涉及到的技术点
- SharedPreferences的使用
2. 效果图
3. 实现过程
- 注册布局文件:activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"tools:context=".LoginActivity"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:layout_width="match_parent"android:layout_height="180dp"android:scaleType="centerCrop"android:src="@mipmap/img_login_bg" /><androidx.appcompat.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="wrap_content"app:navigationIcon="@drawable/ic_baseline_arrow_back_24"app:title="注册"app:titleTextColor="@color/white" /></RelativeLayout><androidx.appcompat.widget.LinearLayoutCompatandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="-10dp"android:background="@drawable/img_shape_login_bg"android:orientation="vertical"><androidx.appcompat.widget.LinearLayoutCompatandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="30dp"android:layout_marginTop="30dp"android:layout_marginRight="30dp"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="欢迎注册"android:textColor="#FF7F00"android:textSize="24sp"android:textStyle="bold" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="注册您的账号!"android:textColor="#BDBDBD"android:textSize="13sp" /><androidx.appcompat.widget.LinearLayoutCompatandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="46dp"android:text="用户名"android:textColor="#585858" /><EditTextandroid:id="@+id/et_username"android:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="6dp"android:background="@drawable/login_et_bg"android:hint="请输入用户名"android:paddingLeft="10dp"android:paddingRight="10dp"android:textSize="14sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="16dp"android:text="密码"android:textColor="#585858" /><EditTextandroid:id="@+id/et_password"android:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="6dp"android:background="@drawable/login_et_bg"android:hint="请输入密码"android:inputType="textPassword"android:paddingLeft="10dp"android:paddingRight="10dp"android:textSize="14sp" /><Buttonandroid:id="@+id/register"android:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="20dp"android:background="@drawable/login_et_bg"android:text="注册" /></androidx.appcompat.widget.LinearLayoutCompat></androidx.appcompat.widget.LinearLayoutCompat></androidx.appcompat.widget.LinearLayoutCompat></androidx.appcompat.widget.LinearLayoutCompat>
- 注册页具体代码:RegisterActivity
public class RegisterActivity extends AppCompatActivity {private EditText et_username;private EditText et_password;private SharedPreferences mSharedPreferences;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register);//获取mSharedPreferencesmSharedPreferences = getSharedPreferences("user", MODE_PRIVATE);//初始化控件et_username = findViewById(R.id.et_username);et_password = findViewById(R.id.et_password);//注册findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String username = et_username.getText().toString();String password = et_password.getText().toString();if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {Toast.makeText(RegisterActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();} else {SharedPreferences.Editor edit = mSharedPreferences.edit();edit.putString("username", username);edit.putString("password", password);//一定要提交edit.commit();Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();finish();}}});//返回findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {finish();}});}
}
- 登录布局文件:activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"tools:context=".LoginActivity"><androidx.core.widget.NestedScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><androidx.appcompat.widget.LinearLayoutCompatandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:layout_width="match_parent"android:layout_height="180dp"android:scaleType="centerCrop"android:src="@mipmap/img_login_bg" /><androidx.appcompat.widget.Toolbarandroid:layout_width="match_parent"android:layout_height="wrap_content"app:title="登录"app:titleTextColor="@color/white" /></RelativeLayout><androidx.appcompat.widget.LinearLayoutCompatandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="-10dp"android:background="@drawable/img_shape_login_bg"android:orientation="vertical"><androidx.appcompat.widget.LinearLayoutCompatandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="30dp"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="欢迎回来"android:textColor="#FF7F00"android:textSize="24sp"android:textStyle="bold" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="继续登录您的账号!"android:textColor="#BDBDBD"android:textSize="13sp" /><androidx.appcompat.widget.LinearLayoutCompatandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="46dp"android:text="用户名"android:textColor="#585858" /><EditTextandroid:id="@+id/et_username"android:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="6dp"android:background="@drawable/login_et_bg"android:hint="请输入用户名"android:paddingLeft="10dp"android:paddingRight="10dp"android:textSize="14sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="16dp"android:text="密码"android:textColor="#585858" /><EditTextandroid:id="@+id/et_password"android:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="6dp"android:background="@drawable/login_et_bg"android:hint="请输入密码"android:inputType="textPassword"android:paddingLeft="10dp"android:paddingRight="10dp"android:textSize="14sp" /><CheckBoxandroid:id="@+id/checkbox"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="记住密码" /><Buttonandroid:id="@+id/login"android:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="20dp"android:background="@drawable/login_et_bg"android:text="登录" /><TextViewandroid:id="@+id/register"android:layout_width="wrap_content"android:layout_height="30dp"android:layout_gravity="center"android:layout_marginTop="40dp"android:gravity="center"android:text="还没有账号? 注册"android:textColor="#747481" /></androidx.appcompat.widget.LinearLayoutCompat></androidx.appcompat.widget.LinearLayoutCompat></androidx.appcompat.widget.LinearLayoutCompat></androidx.appcompat.widget.LinearLayoutCompat></androidx.core.widget.NestedScrollView></androidx.appcompat.widget.LinearLayoutCompat>
- 登录页具体代码:LoginActivity
public class LoginActivity extends AppCompatActivity {private EditText et_username;private EditText et_password;private SharedPreferences mSharedPreferences;private CheckBox checkbox;private boolean is_login;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);//获取mSharedPreferencesmSharedPreferences = getSharedPreferences("user", MODE_PRIVATE);//初始化控件et_username = findViewById(R.id.et_username);et_password = findViewById(R.id.et_password);checkbox = findViewById(R.id.checkbox);//判断是否记住了密码is_login = mSharedPreferences.getBoolean("is_login", false);if (is_login) {et_username.setText(mSharedPreferences.getString("username", null));et_password.setText(mSharedPreferences.getString("password", null));checkbox.setChecked(true);}//注册findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//跳转到注册页面startActivity(new Intent(LoginActivity.this, RegisterActivity.class));}});//登录findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String username = et_username.getText().toString();String password = et_password.getText().toString();if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();} else {String name = mSharedPreferences.getString("username", null);String pwd = mSharedPreferences.getString("password", null);if (username.equals(name) && password.equals(pwd)) {//保存登录状态SharedPreferences.Editor editor = mSharedPreferences.edit();editor.putBoolean("is_login", is_login);editor.putString("username", username);editor.putString("password", password);editor.apply();//跳转到主页面Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();startActivity(new Intent(LoginActivity.this, NewsMainActivity.class));finish();} else {Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();}}}});checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {is_login = b;}});}
}
xml中所设计的图片资源,请自行替换即可