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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerCrop"
android:src="@mipmap/img_login_bg" />
<androidx.appcompat.widget.Toolbar
android: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.LinearLayoutCompat
android: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.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginRight="30dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="欢迎注册"
android:textColor="#FF7F00"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="注册您的账号!"
android:textColor="#BDBDBD"
android:textSize="13sp" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="46dp"
android:text="用户名"
android:textColor="#585858" />
<EditText
android: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" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="16dp"
android:text="密码"
android:textColor="#585858" />
<EditText
android: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" />
<Button
android: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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//获取mSharedPreferences
mSharedPreferences = 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() {
@Override
public 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() {
@Override
public 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.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
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:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerCrop"
android:src="@mipmap/img_login_bg" />
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="登录"
app:titleTextColor="@color/white" />
</RelativeLayout>
<androidx.appcompat.widget.LinearLayoutCompat
android: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.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="欢迎回来"
android:textColor="#FF7F00"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="继续登录您的账号!"
android:textColor="#BDBDBD"
android:textSize="13sp" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="46dp"
android:text="用户名"
android:textColor="#585858" />
<EditText
android: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" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="16dp"
android:text="密码"
android:textColor="#585858" />
<EditText
android: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" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:background="@drawable/login_et_bg"
android:text="登录" />
<TextView
android: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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//获取mSharedPreferences
mSharedPreferences = 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() {
@Override
public void onClick(View view) {
//跳转到注册页面
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
});
//登录
findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
@Override
public 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() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
is_login = b;
}
});
}
}
xml中所设计的图片资源,请自行替换即可