Compare commits

..

2 Commits

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

@ -1,20 +1,28 @@
package cc.liuyx.app;
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;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText usernameEdit, passwdEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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.setOnClickListener(new View.OnClickListener() {
@Override
@ -22,13 +30,42 @@ public class LoginActivity extends AppCompatActivity {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
});
}
Button btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, UserInfoActivity.class));
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:
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;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
back.setOnClickListener(new View.OnClickListener() {
@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;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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" />
<EditText
android:id="@+id/editTextTextPersonName"
android:id="@+id/login_username"
android:layout_width="357dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
@ -35,7 +35,7 @@
android:textSize="15sp" />
<EditText
android:id="@+id/editTextTextPassword"
android:id="@+id/login_passwd"
android:layout_width="357dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"

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

@ -11,8 +11,8 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_marginBottom="35dp"
android:layout_marginTop="45dp"
android:layout_marginBottom="30dp"
android:gravity="center">
<ImageView
@ -22,7 +22,64 @@
</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_height="wrap_content"
android:layout_marginBottom="15dp"
@ -33,14 +90,15 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_edit" />
android:src="@drawable/ic_email" />
<TextView
android:id="@+id/user_email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:text=" 修改信息"
android:textSize="16sp" />
android:text=" 邮箱:"
android:textSize="@dimen/normal_font_size" />
<ImageView
android:layout_width="0dp"
@ -70,7 +128,7 @@
android:layout_height="wrap_content"
android:layout_weight="8"
android:text=" 设置"
android:textSize="16sp" />
android:textSize="@dimen/normal_font_size" />
<ImageView
android:layout_width="0dp"
@ -99,7 +157,7 @@
android:layout_height="wrap_content"
android:layout_weight="8"
android:text=" 更多"
android:textSize="16sp" />
android:textSize="@dimen/normal_font_size" />
<ImageView
android:layout_width="0dp"
@ -110,16 +168,35 @@
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center">
</LinearLayout>
<RelativeLayout
android:layout_width="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
android:id="@+id/footnote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:text="联系我liuyxcc@gmail.com"
android:text="@string/footnote"
android:textAlignment="center" />
</RelativeLayout>

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

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