页面跳转、登录注册验证

master
liuyx 2 years ago
parent b515b1d779
commit 2379c7e7d6

@ -13,7 +13,7 @@
tools:targetApi="31"> tools:targetApi="31">
<activity <activity
android:name=".LoginActivity" android:name=".UserInfoActivity"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@ -26,7 +26,7 @@
android:exported="false" /> android:exported="false" />
<activity <activity
android:name=".UserInfoActivity" android:name=".LoginActivity"
android:exported="false" /> android:exported="false" />
</application> </application>

@ -1,20 +1,28 @@
package cc.liuyx.app; package cc.liuyx.app;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity { public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText usernameEdit, passwdEdit;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login); setContentView(R.layout.activity_login);
usernameEdit = findViewById(R.id.login_username);
passwdEdit = findViewById(R.id.login_passwd);
findViewById(R.id.btn_login).setOnClickListener(this);
TextView textView = findViewById(R.id.click_register); TextView textView = findViewById(R.id.click_register);
textView.setOnClickListener(new View.OnClickListener() { textView.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -22,13 +30,42 @@ public class LoginActivity extends AppCompatActivity {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class)); startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
} }
}); });
}
Button btnLogin = findViewById(R.id.btn_login); @Override
btnLogin.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
@Override switch (v.getId()) {
public void onClick(View view) { case R.id.btn_login:
startActivity(new Intent(LoginActivity.this, UserInfoActivity.class)); login();
break;
}
}
private void login() {
String username = usernameEdit.getText().toString();
String passwd = passwdEdit.getText().toString();
if (!username.equals("") && !passwd.equals("")) {
SharedPreferences sp = getSharedPreferences("loginStatus", MODE_PRIVATE);
String localName = sp.getString("username", null);
String localPasswd = sp.getString("passwd", null);
if (!username.equals(localName) || !passwd.equals(localPasswd)) {
Toast.makeText(LoginActivity.this, "用户名或密码错误>_<", Toast.LENGTH_SHORT).show();
return;
} }
});
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("isLoggedIn", true);
editor.putString("username", username);
editor.apply();
Intent intent = new Intent(LoginActivity.this, UserInfoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "请填入用户名与密码哦~", Toast.LENGTH_SHORT).show();
}
} }
} }

@ -1,18 +1,40 @@
package cc.liuyx.app; package cc.liuyx.app;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
public class RegisterActivity extends AppCompatActivity { public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private String username = "";
private String passwd = "";
private String passwdAgain = "";
private String phone = "";
private String email = "";
private EditText usernameEdit, passwdEdit, passwdAgainEdit, phoneEdit, emailEdit;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register); setContentView(R.layout.activity_register);
// 初始化每个组件
usernameEdit = findViewById(R.id.username);
passwdEdit = findViewById(R.id.passwd);
passwdAgainEdit = findViewById(R.id.passwd2);
phoneEdit = findViewById(R.id.phone);
emailEdit = findViewById(R.id.email);
findViewById(R.id.btn_register).setOnClickListener(this);
ImageView back = findViewById(R.id.register_back); ImageView back = findViewById(R.id.register_back);
back.setOnClickListener(new View.OnClickListener() { back.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -22,4 +44,43 @@ public class RegisterActivity extends AppCompatActivity {
} }
}); });
} }
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_register) {
validateRegister();
}
}
private void validateRegister() {
Intent intent = new Intent(RegisterActivity.this, UserInfoActivity.class);
username = usernameEdit.getText().toString();
passwd = passwdEdit.getText().toString();
passwdAgain = passwdAgainEdit.getText().toString();
email = emailEdit.getText().toString();
phone = phoneEdit.getText().toString();
// 判断两次密码是否相等
if (passwd.equals(passwdAgain)) {
if (!username.equals("") && !passwd.equals("")) {
// 设置登录状态
SharedPreferences sharedPreferences = getSharedPreferences("loginStatus", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isLoggedIn", true);
editor.putString("username", username);
editor.putString("email", email);
editor.putString("phone", phone);
editor.putString("passwd", passwd);
editor.apply();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Toast.makeText(RegisterActivity.this, "请填入用户名与密码哦~", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(RegisterActivity.this, "两次密码输入不一致!", Toast.LENGTH_SHORT).show();
}
}
} }

@ -1,14 +1,65 @@
package cc.liuyx.app; package cc.liuyx.app;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
public class UserInfoActivity extends AppCompatActivity { public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener {
private String username = "";
private String phone = "";
private String email = "";
@SuppressLint("SetTextI18n")
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_info); setContentView(R.layout.activity_user_info);
// 判断用户是否已经登录
// 如果未登录,则跳转至登录页面
SharedPreferences preferences = getSharedPreferences("loginStatus", MODE_PRIVATE);
boolean isLogin = preferences.getBoolean("isLoggedIn", false);
System.out.println(isLogin);
if (!isLogin) {
// 跳转到 LoginActivity
Intent intent = new Intent(UserInfoActivity.this, LoginActivity.class);
startActivity(intent);
finish();
return;
}
username = preferences.getString("username", null);
email = preferences.getString("email", null);
phone = preferences.getString("phone", null);
TextView userName = findViewById(R.id.user_username);
TextView userPhone = findViewById(R.id.user_phone);
TextView userEmail = findViewById(R.id.user_email);
userName.setText(userName.getText() + username);
userPhone.setText(userPhone.getText() + phone);
userEmail.setText(userEmail.getText() + email);
Button btnLogout = findViewById(R.id.logout);
btnLogout.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.logout) {
SharedPreferences preferences = getSharedPreferences("loginStatus", MODE_PRIVATE);
SharedPreferences.Editor edit = preferences.edit();
edit.remove("isLoggedIn");
edit.apply();
finish();
startActivity(new Intent(this, LoginActivity.class));
}
} }
} }

@ -23,7 +23,7 @@
android:textSize="18sp" /> android:textSize="18sp" />
<EditText <EditText
android:id="@+id/editTextTextPersonName" android:id="@+id/login_username"
android:layout_width="357dp" android:layout_width="357dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="10dp" android:layout_margin="10dp"
@ -35,7 +35,7 @@
android:textSize="15sp" /> android:textSize="15sp" />
<EditText <EditText
android:id="@+id/editTextTextPassword" android:id="@+id/login_passwd"
android:layout_width="357dp" android:layout_width="357dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="10dp" android:layout_margin="10dp"

@ -41,7 +41,7 @@
android:hint="来将可留姓名?" android:hint="来将可留姓名?"
android:inputType="textPersonName" android:inputType="textPersonName"
android:lines="1" android:lines="1"
android:textSize="15sp" /> android:textSize="@dimen/normal_font_size" />
<EditText <EditText
android:id="@+id/passwd" android:id="@+id/passwd"
@ -54,7 +54,7 @@
android:hint="对对暗号" android:hint="对对暗号"
android:inputType="textPassword" android:inputType="textPassword"
android:lines="1" android:lines="1"
android:textSize="15sp" /> android:textSize="@dimen/normal_font_size" />
<EditText <EditText
android:id="@+id/passwd2" android:id="@+id/passwd2"
@ -67,7 +67,7 @@
android:hint="重复暗号" android:hint="重复暗号"
android:inputType="textPassword" android:inputType="textPassword"
android:lines="1" android:lines="1"
android:textSize="15sp" /> android:textSize="@dimen/normal_font_size" />
<EditText <EditText
android:id="@+id/phone" android:id="@+id/phone"
@ -80,7 +80,7 @@
android:inputType="phone" android:inputType="phone"
android:lines="1" android:lines="1"
android:textAllCaps="false" android:textAllCaps="false"
android:textSize="15sp" /> android:textSize="@dimen/normal_font_size" />
<EditText <EditText
android:id="@+id/email" android:id="@+id/email"
@ -92,7 +92,7 @@
android:hint="邮箱也留个噻" android:hint="邮箱也留个噻"
android:inputType="textEmailAddress" android:inputType="textEmailAddress"
android:lines="1" android:lines="1"
android:textSize="15sp" /> android:textSize="@dimen/normal_font_size" />
<Button <Button
android:id="@+id/btn_register" android:id="@+id/btn_register"

@ -11,8 +11,8 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="55dp" android:layout_marginTop="45dp"
android:layout_marginBottom="35dp" android:layout_marginBottom="30dp"
android:gravity="center"> android:gravity="center">
<ImageView <ImageView
@ -22,7 +22,64 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/user_edit" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_infoitem"
android:gravity="center">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_user" />
<TextView
android:id="@+id/user_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:text=" 用户名:"
android:textSize="@dimen/normal_font_size" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_weight="1"
android:src="@drawable/ic_arrow_right" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_infoitem"
android:gravity="center">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_phone" />
<TextView
android:id="@+id/user_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:text=" 手机号:"
android:textSize="@dimen/normal_font_size" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_weight="1"
android:src="@drawable/ic_arrow_right" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="15dp" android:layout_marginBottom="15dp"
@ -33,14 +90,15 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:src="@drawable/ic_edit" /> android:src="@drawable/ic_email" />
<TextView <TextView
android:id="@+id/user_email"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="8" android:layout_weight="8"
android:text=" 修改信息" android:text=" 邮箱:"
android:textSize="16sp" /> android:textSize="@dimen/normal_font_size" />
<ImageView <ImageView
android:layout_width="0dp" android:layout_width="0dp"
@ -70,7 +128,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="8" android:layout_weight="8"
android:text=" 设置" android:text=" 设置"
android:textSize="16sp" /> android:textSize="@dimen/normal_font_size" />
<ImageView <ImageView
android:layout_width="0dp" android:layout_width="0dp"
@ -99,7 +157,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="8" android:layout_weight="8"
android:text=" 更多" android:text=" 更多"
android:textSize="16sp" /> android:textSize="@dimen/normal_font_size" />
<ImageView <ImageView
android:layout_width="0dp" android:layout_width="0dp"
@ -110,16 +168,35 @@
</LinearLayout> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center">
</LinearLayout>
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<Button
android:id="@+id/logout"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_above="@+id/footnote"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:text="退出登录" />
<TextView <TextView
android:id="@+id/footnote"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp" android:layout_marginBottom="10dp"
android:text="联系我liuyxcc@gmail.com" android:text="@string/footnote"
android:textAlignment="center" /> android:textAlignment="center" />
</RelativeLayout> </RelativeLayout>

@ -3,4 +3,5 @@
<dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen> <dimen name="fab_margin">16dp</dimen>
<dimen name="normal_font_size">16sp</dimen>
</resources> </resources>

@ -10,4 +10,6 @@
<string name="hello_first_fragment">Hello first fragment</string> <string name="hello_first_fragment">Hello first fragment</string>
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string> <string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
<string name="title_activity_register">RegisterActivity</string> <string name="title_activity_register">RegisterActivity</string>
<string name="footnote">联系我liuyxcc@gmail.com</string>
</resources> </resources>
Loading…
Cancel
Save