
由于这个模块内容较多,篇幅较长,请耐心阅读。
注册与登录模块分为三个部分
- [x] 欢迎界面
- [x] 注册界面
- [x] 登录界面
一、欢迎界面
1、创建工程,命名为BoXueGu,包名为com.boxuegu。

2、导入界面图片luunch_bg.png,导入drawable文件夹中。

3、创建欢迎界面
(1)、欢迎界面的实现
在com.boxuegu包下创建activity包,在activity包下新建一个类,命名为SplashActivity。在res/layout下创建xml布局文件,命名为activity_splash。
(2)、activity_splash.xml文件代码如下:
1<?xml version="1.0" encoding="utf-8"?> 2<!-- match_parent:占据手机界面全部 wrap_content:占据自己的那一部分--> 3<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="@drawable/launch_bg"> 7 <!-- 设置欢迎界面背景图片 --> 8 9 <!-- 文本控件用于显示项目版本号 --> 10 <TextView 11 android:id="@+id/tv_version" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:layout_centerInParent="true" 15 android:textColor="@android:color/white" 16 android:textSize="18sp"/> 17</RelativeLayout>
(3)、修改清单文件(AndroidManifest.xml)
修改为自己的icon图标,在AndroidManifest.xml的<application>标签修改icon属性:
1 <application 2 ...... ...... ...... 3 android:icon="@mipmap/app_icon" 4 ...... ...... ......> 5 </application>
项目创建以后所有的界面都要使用自定义颜色的标题栏,所有需要在AndroidManifest.xml的<application>标签修改theme属性:
1 <application 2 ...... ...... ...... 3 android:theme="@style/Theme.AppCompat.NoActionBar"> 4 ...... ...... ...... 5 </application>
修改默认项目启动界面为欢迎界面,将AndroidManifest.xml的 <intent-filter>及标签里面的内容剪切到SplashActivity所在的<activity>标签中
1 <activity android:name=".activity.SplashActivity"> 2 <intent-filter> 3 <action android:name="android.intent.action.MAIN" /> 4 <category android:name="android.intent.category.LAUNCHER" /> 5 </intent-filter> 6</activity>
4、欢迎界面逻辑代码
1package com.boxuegu.activity; 2 3import android.content.Intent; 4import android.content.pm.ActivityInfo; 5import android.content.pm.PackageInfo; 6import android.content.pm.PackageManager; 7import android.os.Bundle; 8import android.support.v7.app.AppCompatActivity; 9import android.widget.TextView; 10import com.boxuegu.R; 11import java.util.Timer; 12import java.util.TimerTask; 13/* 14 继承AppCompatActivity类的原因: 15 比继承activity类界面多了ActionBar 16 Android studio默认继承AppCompatActivity类 17*/ 18public class SplashActivity extends AppCompatActivity { 19 20 private TextView tv_version; //新建tv_version成员变量 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_splash); 26 //设置此界面为竖屏 27 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 28 init(); 29 } 30 31 private void init() { 32 //将布局文件的tv_version属性赋给成员变量tv_version 33 tv_version = (TextView) findViewById(R.id.tv_version); 34 try { 35 //获取程序包信息 36 PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0); 37 //获得版本号:AndroidManifest.xml里面的versionName 38 tv_version.setText("V" + info.versionName); 39 } catch (PackageManager.NameNotFoundException e) { 40 //如果获取不到版本号,控制台打印错误日志,System.err 41 e.printStackTrace(); 42 //欢迎界面返回V 43 tv_version.setText("V"); 44 } 45 //利用Timer让此界面延迟3秒后跳转,timer中有一个线程,这个线程不断执行task 46 Timer timer = new Timer(); 47 //timertask实现runnable接口,TimerTask类表示一个在指定时间内执行的task 48 TimerTask task = new TimerTask() { 49 @Override 50 public void run() { 51 //欢迎界面跳转到主界面 52 Intent intent = new Intent(SplashActivity.this, MainActivity.class); 53 startActivity(intent); 54 SplashActivity.this.finish(); 55 } 56 }; 57 timer.schedule(task, 3000);//设置这个task在延迟3秒之后自动执行 58 } 59}
5、欢迎界面运行效果

二、注册界面
1、制作标题栏
项目中大部分界面都有一个返回按钮和标题栏。为了便于代码重复利用,可以单独制作一个布局文件。
(1)、创建标题栏界面
在res/layout文件夹中,创建main_title_bar.xml。代码如下:
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:id="@+id/title_bar" 6 android:layout_height="50dp" 7 android:background="@android:color/transparent"> 8 9 <!-- 设置标题栏背景颜色为go_back_selector.xml --> 10 <TextView 11 android:id="@+id/tv_back" 12 android:layout_width="50dp" 13 android:layout_height="50dp" 14 android:layout_alignParentLeft="true" 15 android:layout_centerVertical="true" 16 android:background="@drawable/go_back_selector"/> 17 18 <!-- 设置标题栏显示字体样式 --> 19 <TextView 20 android:id="@+id/tv_main_title" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:textColor="@android:color/white" 24 android:textSize="20sp" 25 android:layout_centerInParent="true" 26 /> 27</RelativeLayout>
(2)、创建背景选择器
标题栏中的返回按钮按下与弹起有明显的区别,可以通过背景选择器实现。首先导入图片:iv_bavk_selected.png(按下)、iv_back.png(弹起)。选中drawable文件夹,新建一个xml文件。代码如下:
1<?xml version="1.0" encoding="utf-8"?> 2<!-- 设置标题栏返回按钮背景选择器:图片和默认背景颜色图片 --> 3<selector xmlns:android="http://schemas.android.com/apk/res/android"> 4 <item android:state_pressed="true" android:drawable="@drawable/iv_back_selected"></item> 5 <item android:drawable="@drawable/iv_back"></item> 6</selector>
2、制作注册界面
(1)、创建注册界面
在com.boxuegu.activity包中创建一个类,命名为RegisterActivity。在在res/layout文件夹中,创建activity_register.xml
(2)、导入界面图片
注册界面所需要图片:register_bg.png(背景图片)、default_icon.png(注册界面头像)、user_name_icon.png(用户名图标)、psw_icon.png(密码图标)、register_user_name_bg(输入用户名)、register_psw_bg(输入密码)、register_psw_again_bg(确认密码)———导入drawable文件夹
(3)、activity_register.xml界面代码
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@drawable/register_bg" 6 android:orientation="vertical"> 7 8 <!-- 导入标题栏——main_title_bar.xml文件 --> 9 <include layout="@layout/main_title_bar"></include> 10 11 <!-- 注册界面头像——default_icon.png --> 12 <ImageView 13 android:layout_width="70dp" 14 android:layout_height="70dp" 15 android:layout_gravity="center_horizontal" 16 android:layout_marginTop="25dp" 17 android:src="@drawable/default_icon" /> 18 19 <!-- 用户名输入框 --> 20 <EditText 21 android:id="@+id/et_user_name" 22 android:layout_width="fill_parent" 23 android:layout_height="48dp" 24 android:layout_marginLeft="35dp" 25 android:layout_marginRight="35dp" 26 android:layout_marginTop="35dp" 27 android:background="@drawable/register_user_name_bg" 28 android:drawableLeft="@drawable/user_name_icon" 29 android:drawablePadding="10dp" 30 android:gravity="center_vertical" 31 android:hint="请输入用户名" 32 android:paddingLeft="8dp" 33 android:singleLine="true" 34 android:textColor="#000000" 35 android:textColorHint="#a3a3a3" 36 android:textSize="14sp"/> 37 38 <!-- 密码输入框 --> 39 <EditText 40 android:id="@+id/et_psw" 41 android:layout_width="fill_parent" 42 android:layout_height="48dp" 43 android:gravity="center_vertical" 44 android:layout_marginLeft="35dp" 45 android:layout_marginRight="35dp" 46 android:background="@drawable/register_psw_bg" 47 android:drawableLeft="@drawable/psw_icon" 48 android:drawablePadding="10dp" 49 android:hint="请输入密码" 50 android:inputType="textPassword" 51 android:paddingLeft="8dp" 52 android:singleLine="true" 53 android:textColor="#000000" 54 android:textColorHint="#a3a3a3" 55 android:textSize="14sp"/> 56 57 <!-- 确认密码输入框 --> 58 <EditText 59 android:id="@+id/et_psw_again" 60 android:layout_width="fill_parent" 61 android:layout_height="48dp" 62 android:gravity="center_vertical" 63 android:layout_marginLeft="35dp" 64 android:layout_marginRight="35dp" 65 android:background="@drawable/register_psw_again_bg" 66 android:drawableLeft="@drawable/psw_icon" 67 android:drawablePadding="10dp" 68 android:hint="请再次输入密码" 69 android:inputType="textPassword" 70 android:paddingLeft="8dp" 71 android:singleLine="true" 72 android:textColor="#000000" 73 android:textColorHint="#a3a3a3" 74 android:textSize="14sp"/> 75 76 <!-- 注册按钮——使用背景选择器register_selector.xml --> 77 <Button 78 android:id="@+id/btn_register" 79 android:layout_width="fill_parent" 80 android:layout_height="40dp" 81 android:layout_gravity="center_horizontal" 82 android:layout_marginLeft="35dp" 83 android:layout_marginRight="35dp" 84 android:layout_marginTop="15dp" 85 android:background="@drawable/register_selector" 86 android:text="注 册" 87 android:textColor="@android:color/white" 88 android:textSize="18sp"/> 89</LinearLayout>
(4)、创建背景选择器
注册按钮按下与弹起有明显的区别,将图片register_icon_normal.png(弹起)、register_icon_selected.png(按下)导入drawable文件夹中。并在该文件夹中创建背景选择器register_selected.xml文件。
1<?xml version="1.0" encoding="utf-8"?> 2<!-- 注册按钮背景选择器按下为register_icon_selected.png,弹起为register_icon_normal.png --> 3<selector xmlns:android="http://schemas.android.com/apk/res/android"> 4 <item android:drawable="@drawable/register_icon_selected" android:state_pressed="true"/> 5 <item android:drawable="@drawable/register_icon_normal"/> 6</selector>
3、MD5加密算法
MD5的全称是Message-Digest ALgorithm 5(信息摘要算法第5版)。把任意长度的字符串变换成固定长度的十六进制字符串。注册与登录都会使用MD5加密算法对密码进行简单的加密。
选择com.boxuegu包,创建一个utils包(工具包),在utils包中创建一个类MD5Utils。代码如下:
1package com.boxuegu.utils; 2 3import java.security.MessageDigest; 4import java.security.NoSuchAlgorithmException; 5 6public class MD5Utils { 7 //创建md5方法对密码进行加密 8 public static String md5(String text){ 9 try { 10 //获取加密对象digest 11 MessageDigest digest = MessageDigest.getInstance("md5"); 12 //digest方法对密码进行加密 13 byte[] result = digest.digest(text.getBytes()); 14 StringBuilder sb = new StringBuilder(); 15 for (byte b : result){ 16 int number = b&0xff; 17 String hex = Integer.toHexString(number); 18 if (hex.length()==1){ 19 sb.append("0"+hex); 20 }else{ 21 sb.append(hex); 22 } 23 } 24 return sb.toString(); 25 } catch (NoSuchAlgorithmException e) { 26 e.printStackTrace(); 27 return ""; 28 } 29 } 30} 31 32
4、注册界面逻辑代码——RegisterActivity.java
1package com.boxuegu.activity; 2 3import android.content.Intent; 4import android.content.SharedPreferences; 5import android.content.pm.ActivityInfo; 6import android.graphics.Color; 7import android.support.v7.app.AppCompatActivity; 8import android.os.Bundle; 9import android.text.TextUtils; 10import android.view.View; 11import android.widget.Button; 12import android.widget.EditText; 13import android.widget.RelativeLayout; 14import android.widget.TextView; 15import android.widget.Toast; 16 17import com.boxuegu.R; 18import com.boxuegu.utils.MD5Utils; 19 20 21public class RegisterActivity extends AppCompatActivity { 22 //标题栏 23 private TextView tv_main_title; 24 //返回按钮 25 private TextView tv_back; 26 //注册按钮 27 private Button btn_register; 28 //账号、密码、再次输入密码的控件 29 private EditText et_user_name,et_psw,et_psw_again; 30 //账号、密码、再次输入密码的控件的获取值 31 private String userName,psw,pswAgain; 32 //标题布局 33 private RelativeLayout rl_rirle_bar; 34 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 //设置页面布局 39 setContentView(R.layout.activity_register); 40 //设置界面为竖屏 41 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 42 init(); 43 } 44 45 private void init(){ 46 //main_title_bar.xml页面布局文件获取对应的UI控件 47 tv_main_title = (TextView)findViewById(R.id.tv_main_title); 48 tv_main_title.setText("注册"); 49 tv_back = (TextView)findViewById(R.id.tv_back); 50 rl_rirle_bar = (RelativeLayout)findViewById(R.id.title_bar); 51 rl_rirle_bar.setBackgroundColor(Color.TRANSPARENT); 52 //activity_register.xml页面布局文件获取对应的UI控件 53 btn_register = (Button)findViewById(R.id.btn_register); 54 et_user_name = (EditText) findViewById(R.id.et_user_name); 55 et_psw =(EditText) findViewById(R.id.et_psw); 56 et_psw_again = (EditText)findViewById(R.id.et_psw_again); 57 tv_back.setOnClickListener(new View.OnClickListener() { 58 @Override 59 public void onClick(View v) { 60 RegisterActivity.this.finish(); 61 } 62 }); 63 btn_register.setOnClickListener(new View.OnClickListener() { 64 @Override 65 public void onClick(View v) { 66 //获取输入在相应控件中的字符串 67 getEditString(); 68 if (TextUtils.isEmpty(userName)){ 69 Toast.makeText(RegisterActivity.this,"请输入用户名",Toast.LENGTH_LONG).show(); 70 return; 71 }else if (TextUtils.isEmpty(psw)){ 72 Toast.makeText(RegisterActivity.this,"请输入密码",Toast.LENGTH_LONG).show(); 73 return; 74 }else if (TextUtils.isEmpty(pswAgain)){ 75 Toast.makeText(RegisterActivity.this,"请再次输入密码",Toast.LENGTH_LONG).show(); 76 return; 77 }else if (!psw.equals(pswAgain)){ 78 Toast.makeText(RegisterActivity.this,"两次的密码不一样",Toast.LENGTH_LONG).show(); 79 return; 80 }else if (isExistUserName(userName)){ 81 Toast.makeText(RegisterActivity.this,"此账户名已经存在",Toast.LENGTH_LONG).show(); 82 return; 83 }else { 84 Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show(); 85 saveRegisterInfo(userName,psw); 86 Intent data = new Intent(); 87 data.putExtra("userName",userName); 88 setResult(RESULT_OK,data); 89 RegisterActivity.this.finish(); 90 } 91 } 92 }); 93 } 94 95 /** 96 * 获取控件中的字符串 97 */ 98 private void getEditString() { 99 userName = et_user_name.getText().toString().trim(); 100 psw = et_psw.getText().toString().trim(); 101 pswAgain = et_psw_again.getText().toString().trim(); 102 } 103 104 /** 105 * 从SharedPreferences中获取用户名,并判断此用户名是否存在 106 */ 107 private boolean isExistUserName(String userName) { 108 boolean has_userName = false; 109 SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE); 110 String spPwd = sp.getString(userName,""); 111 if (!TextUtils.isEmpty(spPwd)){ 112 has_userName = true; 113 } 114 return has_userName; 115 } 116 117 118 /** 119 * 保存用户名和密码到SharedPreferences中 120 */ 121 private void saveRegisterInfo(String userName, String psw) { 122 //把密码用MD5加密 123 String md5Psw = MD5Utils.md5(psw); 124 //loginInfo表示文件名 125 SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE); 126 SharedPreferences.Editor editor = sp.edit(); //获取编辑器 127 //以用户名为key,密码为value保存到SharedPreferences 128 editor.putString(userName,md5Psw); 129 editor.apply(); //提交修改 130 } 131} 132
5、修改欢迎界面代码,使欢迎界面跳转到注册界面(测试过程)

6、测试效果

三、登录界面
1、创建登录界面
在com.boxuegu.activity包中创建一个类,命名为LoginActivity。在res/layout下创建一个xml文件,命名为activity_login。
2、导入界面图片
导入drawable文件夹:login_bg.png(背景图片)、login_user_name_bg.png(输入用户名)、login_psw_bg.png(输入密码)。
3、界面代码activity_login.xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@drawable/login_bg" 6 android:orientation="vertical"> 7 <!-- 导入标题栏——main_title_bar.xml文件 --> 8 <include layout="@layout/main_title_bar" /> 9 10 <!-- 登录界面头像——default_icon.png --> 11 <ImageView 12 android:id="@+id/iv_head" 13 android:layout_width="70dp" 14 android:layout_height="70dp" 15 android:layout_marginTop="25dp" 16 android:layout_gravity="center_horizontal" 17 android:background="@drawable/default_icon" /> 18 19 <!-- 用户名输入框 --> 20 <EditText 21 android:id="@+id/et_user_name" 22 android:layout_width="fill_parent" 23 android:layout_height="48dp" 24 android:layout_marginTop="35dp" 25 android:layout_marginLeft="35dp" 26 android:layout_marginRight="35dp" 27 android:layout_gravity="center_horizontal" 28 android:background="@drawable/login_user_name_bg" 29 android:drawableLeft="@drawable/user_name_icon" 30 android:drawablePadding="10dp" 31 android:paddingLeft="8dp" 32 android:gravity="center_vertical" 33 android:hint="请输入用户名" 34 android:singleLine="true" 35 android:textColor="#000000" 36 android:textColorHint="#a3a3a3" 37 android:textSize="14sp" /> 38 39 <!-- 密码输入框 --> 40 <EditText 41 android:id="@+id/et_psw" 42 android:layout_width="fill_parent" 43 android:layout_height="48dp" 44 android:layout_gravity="center_horizontal" 45 android:layout_marginRight="35dp" 46 android:layout_marginLeft="35dp" 47 android:background="@drawable/login_psw_bg" 48 android:drawableLeft="@drawable/psw_icon" 49 android:drawablePadding="10dp" 50 android:paddingLeft="8dp" 51 android:hint="请输入密码" 52 android:inputType="textPassword" 53 android:singleLine="true" 54 android:textColor="#000000" 55 android:textColorHint="#a3a3a3" 56 android:textSize="14sp" /> 57 58 <!-- 登录按钮 --> 59 <Button 60 android:id="@+id/btn_login" 61 android:layout_width="fill_parent" 62 android:layout_height="40dp" 63 android:layout_marginTop="15dp" 64 android:layout_marginLeft="35dp" 65 android:layout_marginRight="35dp" 66 android:layout_gravity="center_horizontal" 67 android:background="@drawable/register_selector" 68 android:text="登 录" 69 android:textColor="@android:color/white" 70 android:textSize="18sp" /> 71 72 <!-- 立即注册和找回密码版块 --> 73 <LinearLayout 74 android:layout_width="fill_parent" 75 android:layout_height="fill_parent" 76 android:layout_marginTop="8dp" 77 android:layout_marginLeft="35dp" 78 android:layout_marginRight="35dp" 79 android:layout_gravity="center_horizontal" 80 android:orientation="horizontal"> 81 82 <!-- 跳转到注册界面按钮 --> 83 <TextView 84 android:id="@+id/tv_register" 85 android:layout_width="0dp" 86 android:layout_height="wrap_content" 87 android:layout_weight="1" 88 android:gravity="center_horizontal" 89 android:padding="8dp" 90 android:text="立即注册" 91 android:textColor="@android:color/white" 92 android:textSize="14sp" /> 93 94 <!-- 跳转到找回密码界面按钮 --> 95 <TextView 96 android:id="@+id/tv_find_psw" 97 android:layout_width="0dp" 98 android:layout_height="wrap_content" 99 android:layout_weight="1" 100 android:gravity="center_horizontal" 101 android:padding="8dp" 102 android:text="找回密码" 103 android:textColor="@android:color/white" 104 android:textSize="14sp" /> 105 </LinearLayout> 106 107</LinearLayout> 108
4、登录界面逻辑代码——LoginActivity.java
1package com.boxuegu.activity; 2 3import android.content.Intent; 4import android.content.SharedPreferences; 5import android.content.pm.ActivityInfo; 6import android.support.v7.app.AppCompatActivity; 7import android.os.Bundle; 8import android.text.TextUtils; 9import android.view.View; 10import android.widget.Button; 11import android.widget.EditText; 12import android.widget.TextView; 13import android.widget.Toast; 14 15import com.boxuegu.R; 16import com.boxuegu.utils.MD5Utils; 17 18 19public class LoginActivity extends AppCompatActivity { 20 21 private TextView tv_main_title; 22 private TextView tv_back,tv_register,tv_find_psw; 23 private Button btn_login; 24 private String userName,psw,spPsw; 25 private EditText et_user_name,et_psw; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 //设置页面布局 31 setContentView(R.layout.activity_login); 32 //设置界面为竖屏 33 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 34 init(); 35 } 36 37 /** 38 * 获取界面控件 39 */ 40 private void init() { 41 tv_main_title = (TextView)findViewById(R.id.tv_main_title); 42 tv_main_title.setText("登录"); 43 tv_back = (TextView)findViewById(R.id.tv_back); 44 tv_register = (TextView)findViewById(R.id.tv_register); 45 tv_find_psw = (TextView)findViewById(R.id.tv_find_psw); 46 btn_login = (Button) findViewById(R.id.btn_login); 47 et_user_name = (EditText)findViewById(R.id.et_user_name); 48 et_psw = (EditText)findViewById(R.id.et_psw); 49 //返回按钮的点击事件 50 tv_back.setOnClickListener(new View.OnClickListener(){ 51 @Override 52 public void onClick(View view) { 53 LoginActivity.this.finish(); 54 } 55 }); 56 57 //立即注册控件的点击事件 58 tv_register.setOnClickListener(new View.OnClickListener() { 59 @Override 60 public void onClick(View view) { 61 //跳转到注册界面 62 Intent intent = new Intent(LoginActivity.this,RegisterActivity.class); 63 startActivityForResult(intent,1); 64 } 65 }); 66 67 //找回密码控件的点击事件 68 tv_find_psw.setOnClickListener(new View.OnClickListener() { 69 @Override 70 public void onClick(View v) { 71 //跳转到找回密码界面(暂时还未创建) 72 } 73 }); 74 75 //登录按钮的点击事件 76 btn_login.setOnClickListener(new View.OnClickListener() { 77 @Override 78 public void onClick(View view) { 79 userName = et_user_name.getText().toString().trim(); 80 psw = et_psw.getText().toString().trim(); 81 String md5Psw = MD5Utils.md5(psw); 82 spPsw = readPsw(userName); 83 //如果用户名为空 84 if (TextUtils.isEmpty(userName)){ 85 Toast.makeText(LoginActivity.this,"请输入用户名",Toast.LENGTH_LONG).show(); 86 return; 87 }else if (TextUtils.isEmpty(psw)){ //如果密码为空 88 Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_LONG).show(); 89 return; 90 }else if (md5Psw.equals(spPsw)){ 91 Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show(); 92 //保存登录状态和登录的用户名 93 saveLoginStatus(true,userName); 94 //把登录成功的状态传递到MainActivity中 95 Intent data = new Intent(); 96 data.putExtra("isLogin",true); 97 data.putExtra("userName",userName); 98 setResult(RESULT_OK,data); 99 LoginActivity.this.finish(); 100 return; 101 }else if (!TextUtils.isEmpty(spPsw) && !md5Psw.equals(spPsw)){ 102 Toast.makeText(LoginActivity.this,"输入的用户名和密码不一致",Toast.LENGTH_LONG).show(); 103 }else { 104 Toast.makeText(LoginActivity.this,"此用户名不存在",Toast.LENGTH_LONG).show(); 105 } 106 } 107 }); 108 } 109 110 /* 111 * 从SharedPreferences根据用户名获取密码 112 */ 113 private String readPsw(String userName){ 114 SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE); 115 return sp.getString(userName,""); 116 } 117 118 /* 119 * 保存登录状态和登录用户名到SharedPreferences中 120 */ 121 private void saveLoginStatus(boolean status, String userName) { 122 SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE); 123 SharedPreferences.Editor editor = sp.edit(); //获取编辑器 124 editor.putBoolean("isLogin",status); 125 editor.putString("loginUserName",userName); //保存登录的用户名 126 editor.commit(); //提交修改 127 } 128 129 @Override 130 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 131 super.onActivityResult(requestCode, resultCode, data); 132 if (data!=null){ 133 //从注册界面传递过来的用户名 134 String userName = data.getStringExtra("userName"); 135 if (!TextUtils.isEmpty(userName)){ 136 et_user_name.setText(userName); 137 //设置光标的位置 138 et_user_name.setSelection(userName.length()); 139 } 140 } 141 } 142} 143
5、修改欢迎界面代码,使欢迎界面跳转到登录界面(测试)

6、效果展示


