LoginActivity.java
package com.example.login;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.angel.GuideActivity;
import com.example.angel.R;
import org.jetbrains.annotations.Nullable;
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "tag";
public static final int REQUEST_CODE_REGISTER = 1;
private Button btnLogin;
private EditText etAccount,etPassword;
private CheckBox cbRemember,cbAutoLogin;
private String userName = "fxjzzyo";
private String pass = "123";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportActionBar().setTitle("登录");
initView();
initData();
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String account = etAccount.getText().toString();
String password = etPassword.getText().toString();
Log.d(TAG, "onClick: -------------" + account);
Log.d(TAG, "password: -------------" + password);
if (TextUtils.isEmpty(userName)) {
Toast.makeText(LoginActivity.this, "还没有注册账号!", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.equals(account, userName)) {
if (TextUtils.equals(password, pass)) {
Toast.makeText(LoginActivity.this, "恭喜你,登录成功!", Toast.LENGTH_LONG).show();
if (cbRemember.isChecked()) {
SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE);
SharedPreferences.Editor edit = spf.edit();
edit.putString("account", account);
edit.putString("password", password);
edit.putBoolean("isRemember", true);
if (cbAutoLogin.isChecked()) {
edit.putBoolean("isLogin", true);
}else {
edit.putBoolean("isLogin", false);
}
edit.apply();
}else {
SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE);
SharedPreferences.Editor edit = spf.edit();
edit.putBoolean("isRemember", false);
edit.apply();
}
Intent intent = new Intent(LoginActivity.this, GuideActivity.class);
intent.putExtra("account", account);
startActivity(intent);
LoginActivity.this.finish();
} else {
Toast.makeText(LoginActivity.this, "密码错误!", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(LoginActivity.this, "用户名错误!", Toast.LENGTH_LONG).show();
}
}
});
cbAutoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
cbRemember.setChecked(true);
}
}
});
cbRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
cbAutoLogin.setChecked(false);
}
}
});
}
private void initView() {
btnLogin = findViewById(R.id.btn_login);
etAccount = findViewById(R.id.et_account);
etPassword = findViewById(R.id.et_password);
cbRemember = findViewById(R.id.cb_remember);
cbAutoLogin = findViewById(R.id.cb_auto_login);
}
private void initData() {
SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE);
boolean isRemember = spf.getBoolean("isRemember", false);
boolean isLogin = spf.getBoolean("isLogin", false);
String account = spf.getString("account", "");
String password = spf.getString("password", "");
if (isLogin) {
Intent intent = new Intent(LoginActivity.this, GuideActivity.class);
intent.putExtra("account", account);
startActivity(intent);
LoginActivity.this.finish();
}
userName = account;
pass = password;
if (isRemember) {
etAccount.setText(account);
etPassword.setText(password);
cbRemember.setChecked(true);
}
}
public void toRegister(View view) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivityForResult(intent, REQUEST_CODE_REGISTER);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_REGISTER && resultCode == RegisterActivity.RESULT_CODE_REGISTER && data != null) {
Bundle extras = data.getExtras();
String account = extras.getString("account", "");
String password = extras.getString("password", "");
etAccount.setText(account);
etPassword.setText(password);
userName = account;
pass = password;
}
}
}
RegisterActivity.Java
package com.example.login;
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.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.angel.R;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
public static final int RESULT_CODE_REGISTER = 0;
private Button btnRegister;
private EditText etAccount,etPass,etPassConfirm;
private CheckBox cbAgree;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
getSupportActionBar().setTitle("注册");
etAccount = findViewById(R.id.et_account);
etPass = findViewById(R.id.et_password);
etPassConfirm = findViewById(R.id.et_password_confirm);
cbAgree = findViewById(R.id.cb_agree);
btnRegister = findViewById(R.id.btn_register);
btnRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name = etAccount.getText().toString();
String pass = etPass.getText().toString();
String passConfirm = etPassConfirm.getText().toString();
if (TextUtils.isEmpty(name)) {
Toast.makeText(RegisterActivity.this, "用户名不能为空", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(pass)) {
Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
return;
}
if (!TextUtils.equals(pass,passConfirm)) {
Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_LONG).show();
return;
}
if (!cbAgree.isChecked()) {
Toast.makeText(RegisterActivity.this, "请同意用户协议", Toast.LENGTH_LONG).show();
return;
}
// 存储注册的用户名 密码
SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE);
SharedPreferences.Editor edit = spf.edit();
edit.putString("account", name);
edit.putString("password", pass);
edit.apply();
// 数据回传
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("account",name);
bundle.putString("password",pass);
intent.putExtras(bundle);
setResult(RESULT_CODE_REGISTER,intent);
Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_LONG).show();
this.finish();
}
}
ic_login_background.png
activity_login.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"
tools:context="com.example.login.LoginActivity"
android:background="@drawable/ic_login_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="25sp"
/>
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:hint="请输入用户名或手机号"
android:layout_marginLeft="10dp"
style="@style/MyEditStyle"
android:inputType="text"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="25sp"
/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入密码"
android:textSize="18sp"
android:layout_marginLeft="10dp"
android:paddingLeft="5dp"
android:inputType="numberPassword"
android:background="@drawable/edit_text_bg"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<CheckBox
android:id="@+id/cb_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
/>
<CheckBox
android:id="@+id/cb_auto_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="自动登录"
android:visibility="visible" />
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
style="@style/MyBtnStyle"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:text="还没有账号?"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:onClick="toRegister"
/>
</LinearLayout>
activity_register.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"
tools:context="com.example.login.RegisterActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账  号:"
android:textSize="25sp" />
<EditText
android:id="@+id/et_account"
style="@style/MyEditStyle"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:hint="请输入用户名或手机号"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密  码:"
android:textSize="25sp"
/>
<EditText
android:id="@+id/et_password"
style="@style/MyEditStyle"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:hint="请输入密码"
android:inputType="numberPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:"
android:textSize="25sp"
/>
<EditText
android:id="@+id/et_password_confirm"
style="@style/MyEditStyle"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:hint="再次输入密码"
android:inputType="numberPassword" />
</LinearLayout>
<Button
android:id="@+id/btn_register"
style="@style/MyBtnStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
<CheckBox
android:id="@+id/cb_agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="勾选即同意用户协议"
android:textColor="@color/colorPrimary" />
</LinearLayout>
效果图: