Initial commit
This commit is contained in:
parent
6f2b709a1f
commit
34a6ac502a
@ -11,6 +11,9 @@
|
|||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.AppCompat.NoActionBar"
|
android:theme="@style/Theme.AppCompat.NoActionBar"
|
||||||
tools:targetApi="31">
|
tools:targetApi="31">
|
||||||
|
<activity
|
||||||
|
android:name=".activity.LoginActivity"
|
||||||
|
android:exported="false" />
|
||||||
<activity
|
<activity
|
||||||
android:name=".activity.RegisterActivity"
|
android:name=".activity.RegisterActivity"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
|
@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import com.youxuegu.activity.LoginActivity;
|
||||||
import com.youxuegu.activity.RegisterActivity;
|
import com.youxuegu.activity.RegisterActivity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -16,7 +17,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
|
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
}
|
}
|
117
app/src/main/java/com/youxuegu/activity/LoginActivity.java
Normal file
117
app/src/main/java/com/youxuegu/activity/LoginActivity.java
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
package com.youxuegu.activity;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.youxuegu.R;
|
||||||
|
import com.youxuegu.utils.MD5Utils;
|
||||||
|
import com.youxuegu.utils.UtilsHelper;
|
||||||
|
|
||||||
|
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
|
||||||
|
|
||||||
|
private TextView tv_main_title;
|
||||||
|
private TextView tv_back, tv_register, tv_find_psw;
|
||||||
|
private Button btn_login;
|
||||||
|
private String userName, psw, spPsw;
|
||||||
|
private EditText et_user_name, et_psw;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_login);
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
tv_main_title = findViewById(R.id.tv_main_title);
|
||||||
|
tv_main_title.setText("登录");
|
||||||
|
tv_back = findViewById(R.id.tv_back);
|
||||||
|
tv_register = findViewById(R.id.tv_register);
|
||||||
|
tv_find_psw = findViewById(R.id.tv_find_psw);
|
||||||
|
btn_login = findViewById(R.id.btn_login);
|
||||||
|
et_user_name = findViewById(R.id.et_user_name);
|
||||||
|
et_psw = findViewById(R.id.et_psw);
|
||||||
|
tv_back.setOnClickListener(this);
|
||||||
|
tv_register.setOnClickListener(this);
|
||||||
|
tv_find_psw.setOnClickListener(this);
|
||||||
|
btn_login.setOnClickListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
|
if (data != null) {
|
||||||
|
//从注册界面回传过来的用户名
|
||||||
|
String userName = data.getStringExtra("userName");
|
||||||
|
if (!TextUtils.isEmpty(userName)) {
|
||||||
|
et_user_name.setText(userName);
|
||||||
|
//设置光标位置
|
||||||
|
et_user_name.setSelection(userName.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
switch (view.getId()) {
|
||||||
|
//"返回"按钮的点击事件
|
||||||
|
case R.id.tv_back:
|
||||||
|
this.finish();
|
||||||
|
break;
|
||||||
|
//"立即注册"文本的点击事件
|
||||||
|
case R.id.tv_register:
|
||||||
|
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
|
||||||
|
startActivityForResult(intent, 1);
|
||||||
|
break;
|
||||||
|
//"找回密码"文本的点击事件
|
||||||
|
case R.id.tv_find_psw:
|
||||||
|
//跳转到找回密码界面
|
||||||
|
//Intent findPswIntent = new Intent(LoginActivity.this, FindPswActivity.class);
|
||||||
|
//startActivity(findPswIntent);
|
||||||
|
break;
|
||||||
|
//"登录"按钮的点击事件
|
||||||
|
case R.id.btn_login:
|
||||||
|
userName = et_user_name.getText().toString().trim();
|
||||||
|
psw = et_psw.getText().toString().trim();
|
||||||
|
String md5Psw = MD5Utils.md5(psw);
|
||||||
|
//根据用户名读取系统中密码
|
||||||
|
spPsw = UtilsHelper.readPsw(LoginActivity.this, userName);
|
||||||
|
if (TextUtils.isEmpty(userName)) {
|
||||||
|
//用户名为空
|
||||||
|
Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
|
} else if (TextUtils.isEmpty(spPsw)) {
|
||||||
|
//用户名未注册
|
||||||
|
Toast.makeText(LoginActivity.this, "此用户名不存在", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
|
} else if (TextUtils.isEmpty(psw)) {
|
||||||
|
//密码未输入
|
||||||
|
Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
|
} else if ((!TextUtils.isEmpty(spPsw) && !md5Psw.equals(spPsw))) {
|
||||||
|
//密码输入错误
|
||||||
|
Toast.makeText(LoginActivity.this, "输入的密码不正确", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
|
} else if (md5Psw.equals(spPsw)) {
|
||||||
|
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
|
||||||
|
//保存登录状态和登录的用户名
|
||||||
|
UtilsHelper.saveLoginStatus(LoginActivity.this, true, userName);
|
||||||
|
//把登录成功的状态传递到MainActivity中
|
||||||
|
Intent data = new Intent();
|
||||||
|
data.putExtra("isLogin", true);
|
||||||
|
setResult(RESULT_OK, data);
|
||||||
|
LoginActivity.this.finish();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -21,6 +21,9 @@ public class UtilsHelper {
|
|||||||
return has_userName;
|
return has_userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将密码用MD5加密
|
||||||
|
*/
|
||||||
public static void saveUserInfo(Context context, String userName, String psw) {
|
public static void saveUserInfo(Context context, String userName, String psw) {
|
||||||
//将密码用MD5加密
|
//将密码用MD5加密
|
||||||
String md5Psw = MD5Utils.md5(psw);
|
String md5Psw = MD5Utils.md5(psw);
|
||||||
@ -32,4 +35,28 @@ public class UtilsHelper {
|
|||||||
editor.putString(userName, md5Psw);
|
editor.putString(userName, md5Psw);
|
||||||
editor.commit();//提交保存信息
|
editor.commit();//提交保存信息
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户名在文件loginInfo中返回密码信息
|
||||||
|
*/
|
||||||
|
public static String readPsw(Context context, String userName) {
|
||||||
|
SharedPreferences sp = context.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
|
||||||
|
String spPsw = sp.getString(userName, "");
|
||||||
|
return spPsw;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存登录状态和登录用户名到SharedPreferences文件中
|
||||||
|
*/
|
||||||
|
public static void saveLoginStatus(Context context, boolean status, String userName) {
|
||||||
|
SharedPreferences sp = context.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
|
||||||
|
//获取编辑器
|
||||||
|
SharedPreferences.Editor editor = sp.edit();
|
||||||
|
//存入boolean类型的登录状态
|
||||||
|
editor.putBoolean("isLogin", status);
|
||||||
|
//存入登录时的用户名
|
||||||
|
editor.putString("loginUserName", userName);
|
||||||
|
//提交修改
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
54
app/src/main/res/layout/activity_login.xml
Normal file
54
app/src/main/res/layout/activity_login.xml
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?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"
|
||||||
|
tools:context=".activity.LoginActivity"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="@drawable/login_bg"
|
||||||
|
android:orientation="vertical">
|
||||||
|
<include layout="@layout/main_title_bar" />
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/iv_head"
|
||||||
|
android:layout_width="70dp"
|
||||||
|
android:layout_height="70dp"
|
||||||
|
android:layout_gravity="center_horizontal"
|
||||||
|
android:layout_marginTop="25dp"
|
||||||
|
android:background="@drawable/default_icon" />
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/et_user_name"
|
||||||
|
style="@style/etRegisterStyle"
|
||||||
|
android:layout_gravity="center_horizontal"
|
||||||
|
android:layout_marginTop="35dp"
|
||||||
|
android:background="@drawable/login_user_name_bg"
|
||||||
|
android:drawableLeft="@drawable/user_name_icon"
|
||||||
|
android:hint="请输入用户名" />
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/et_psw"
|
||||||
|
style="@style/etRegisterStyle"
|
||||||
|
android:background="@drawable/login_psw_bg"
|
||||||
|
android:drawableLeft="@drawable/psw_icon"
|
||||||
|
android:hint="请输入密码"
|
||||||
|
android:inputType="textPassword" />
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btn_login"
|
||||||
|
style="@style/btnRegisterStyle"
|
||||||
|
android:text="登 录" />
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="fill_parent"
|
||||||
|
android:layout_marginLeft="35dp"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:layout_marginRight="35dp"
|
||||||
|
android:gravity="center_horizontal"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_register"
|
||||||
|
style="@style/tvLoginStyle"
|
||||||
|
android:text="立即注册" />
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_find_psw"
|
||||||
|
style="@style/tvLoginStyle"
|
||||||
|
android:text="找回密码?" />
|
||||||
|
</LinearLayout>
|
||||||
|
</LinearLayout>
|
@ -39,4 +39,14 @@
|
|||||||
<item name="android:textColor">@android:color/white</item>
|
<item name="android:textColor">@android:color/white</item>
|
||||||
<item name="android:textSize">18sp</item>
|
<item name="android:textSize">18sp</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
<style name="tvLoginStyle">
|
||||||
|
<item name="android:layout_width">0dp</item>
|
||||||
|
<item name="android:layout_height">wrap_content</item>
|
||||||
|
<item name="android:layout_weight">1</item>
|
||||||
|
<item name="android:gravity">center_horizontal</item>
|
||||||
|
<item name="android:padding">8dp</item>
|
||||||
|
<item name="android:textColor">@android:color/white</item>
|
||||||
|
<item name="android:textSize">14sp</item>
|
||||||
|
</style>
|
||||||
</resources>
|
</resources>
|
Loading…
Reference in New Issue
Block a user