Android仿QQ登录界面示例,实现登录、注册功能。
16lz
2021-01-26
首语
- 历经一个月的时间,自己终于搭建完成了个人网站,还在持续优化中,网站采用halo博客系统,功能非常强大!欢迎大家来我的网站逛逛。有什么建议可以留言!
网站地址:http://www.yanghujun.com
Android开发经常用到注册、登录功能,于是便整理出一般通用的登录界面,并实现其相应功能。供读者参阅。此项目包含三个活动,即登录,注册界面,找回密码。
GitHub源码地址:LoginTest
界面
下面是对代码的分析过程。
首先是登录界面 activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/bg" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="35dp"> <ImageView android:id="@+id/symbol" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_marginLeft="20dp" android:id="@+id/qq" android:layout_width="wrap_content" android:layout_marginTop="35dp" android:text="仿QQ" android:textSize="24sp" android:layout_height="wrap_content" /> LinearLayout> <EditText android:id="@+id/et_user_name" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="60dp" android:hint="账号" android:textSize="20sp" /> <EditText android:id="@+id/et_psw" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:hint="密码" android:textSize="20sp" android:inputType="textPassword"/> <Button android:id="@+id/btn_login" android:text="登录" android:background="#1E90FF" android:textSize="24sp" android:layout_gravity="center" android:layout_marginTop="20dp" android:layout_width="320dp" android:layout_height="wrap_content"/> <RelativeLayout android:layout_marginTop="15dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_register" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新用户注册"/> <TextView android:id="@+id/tv_find_psw" android:layout_alignParentLeft="true" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="忘记密码?" /> RelativeLayout> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_gravity="center" android:layout_marginBottom="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_alignParentBottom="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录即代表阅读并同意服务条款" /> RelativeLayout> LinearLayout>LinearLayout>
对应的就是登录活动 MainActivity:
public class MainActivity extends AppCompatActivity { 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_main); //设置此界面为竖屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); init(); } //获取界面控件 private void init() { //从main_title_bar中获取的id //从activity_login.xml中获取的 TextView tv_register = (TextView) findViewById(R.id.tv_register); TextView tv_find_psw = (TextView) findViewById(R.id.tv_find_psw); Button btn_login = (Button) findViewById(R.id.btn_login); et_user_name= (EditText) findViewById(R.id.et_user_name); et_psw= (EditText) findViewById(R.id.et_psw); //立即注册控件的点击事件 tv_register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //为了跳转到注册界面,并实现注册功能 Intent intent=new Intent(MainActivity.this,RegisterActivity.class); startActivityForResult(intent, 1); } }); //找回密码控件的点击事件 tv_find_psw.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,LostFindActivity.class)); } }); //登录按钮的点击事件 btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //开始登录,获取用户名和密码 getText().toString().trim(); userName = et_user_name.getText().toString().trim(); psw = et_psw.getText().toString().trim(); //对当前用户输入的密码进行MD5加密再进行比对判断, MD5Utils.md5( ); psw 进行加密判断是否一致 String md5Psw = MD5Utils.md5(psw); // md5Psw ; spPsw 为 根据从SharedPreferences中用户名读取密码 // 定义方法 readPsw为了读取用户名,得到密码 spPsw = readPsw(userName); // TextUtils.isEmpty if (TextUtils.isEmpty(userName)) { Toast.makeText(MainActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show(); } else if (TextUtils.isEmpty(psw)) { Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show(); // md5Psw.equals(); 判断,输入的密码加密后,是否与保存在SharedPreferences中一致 } else if (md5Psw.equals(spPsw)) { //一致登录成功 Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); //保存登录状态,在界面保存登录的用户名 定义个方法 saveLoginStatus boolean 状态 , userName 用户名; saveLoginStatus(true, userName); //登录成功后关闭此页面进入主页 Intent data = new Intent(); //datad.putExtra( ); name , value ; data.putExtra("isLogin", true); //RESULT_OK为Activity系统常量,状态码为-1 // 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值 setResult(RESULT_OK, data); //销毁登录界面 MainActivity.this.finish(); //跳转到主界面,登录成功的状态传递到 MainActivity 中 startActivity(new Intent(MainActivity.this, ItemActivity.class)); } else if ((spPsw != null && !TextUtils.isEmpty(spPsw) && !md5Psw.equals(spPsw))) { Toast.makeText(MainActivity.this, "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "此用户名不存在", Toast.LENGTH_SHORT).show(); } } }); } /** *从SharedPreferences中根据用户名读取密码 */ private String readPsw(String userName){ //getSharedPreferences("loginInfo",MODE_PRIVATE); //"loginInfo",mode_private; MODE_PRIVATE表示可以继续写入 SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE); //sp.getString() userName, ""; return sp.getString(userName , ""); } /** *保存登录状态和登录用户名到SharedPreferences中 */ private void saveLoginStatus(boolean status,String userName){ //loginInfo表示文件名 SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE); SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE); //获取编辑器 SharedPreferences.Editor editor=sp.edit(); //存入boolean类型的登录状态 editor.putBoolean("isLogin", status); //存入登录状态时的用户名 editor.putString("loginUserName", userName); //提交修改 editor.apply(); } /** * 注册成功的数据返回至此 * @param requestCode 请求码 * @param resultCode 结果码 * @param data 数据 */ @Override //显示数据, onActivityResult //startActivityForResult(intent, 1); 从注册界面中获取数据 //int requestCode , int resultCode , Intent data // LoginActivity -> startActivityForResult -> onActivityResult(); protected void onActivityResult(int requestCode, int resultCode, Intent data) { //super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data); if(data!=null){ //是获取注册界面回传过来的用户名 // getExtra().getString("***"); String userName=data.getStringExtra("userName"); if(!TextUtils.isEmpty(userName)){ //设置用户名到 et_user_name 控件 et_user_name.setText(userName); //et_user_name控件的setSelection()方法来设置光标位置 et_user_name.setSelection(userName.length()); } } }}
接下来是注册界面 activity_register.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/colorPrimary" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="注册" android:textSize="30sp"/> RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@drawable/shap" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal" > <EditText android:id="@+id/et_user_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:paddingTop="2dp" android:paddingBottom="2dp" android:paddingLeft="6dp" android:hint="账号" android:singleLine="true" android:maxLength="15" android:background="#ffffff" android:textSize="18sp" /> LinearLayout> <LinearLayout android:background="@drawable/shap" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:paddingTop="2dp" android:paddingBottom="2dp" android:paddingLeft="6dp" android:singleLine="true" android:hint="请输入密码" android:inputType="textPassword" android:maxLength="15" android:background="#ffffff" android:id="@+id/et_psw" android:textSize="18sp" /> LinearLayout> <LinearLayout android:background="@drawable/shap" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:paddingTop="2dp" android:paddingBottom="2dp" android:paddingLeft="6dp" android:singleLine="true" android:hint="请再次输入密码" android:inputType="textPassword" android:maxLength="15" android:background="#ffffff" android:id="@+id/et_psw_again" android:textSize="18sp" /> LinearLayout> <LinearLayout android:background="@drawable/shap" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="10dp" android:paddingLeft="6dp" android:gravity="left" android:text="性别" android:textSize="18sp" /> <RadioGroup android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="2.6" android:id="@+id/SexRadio" android:paddingLeft="5dp"> <RadioButton android:id="@+id/mainRegisterRdBtnFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女生"/> <RadioButton android:id="@+id/mainRegisterRdBtnMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男生"/> RadioGroup> LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:orientation="horizontal" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:paddingTop="2dp" android:paddingBottom="2dp" android:paddingLeft="6dp" android:hint="学校" android:singleLine="true" android:maxLength="15" android:background="#ffffff" android:textSize="18sp" /> LinearLayout> LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" > <Button android:id="@+id/btn_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="40dp" android:text="注册" android:textSize="24sp" android:background="@color/colorPrimary" /> RelativeLayout>LinearLayout>
对应注册活动 RegisterActivity:
public class RegisterActivity extends AppCompatActivity { //用户名,密码,再次输入的密码的控件 private EditText et_user_name,et_psw,et_psw_again; //用户名,密码,再次输入的密码的控件的获取值 private String userName,psw,pswAgain; private RadioGroup Sex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置页面布局 ,注册界面 setContentView(R.layout.activity_register); //设置此界面为竖屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); init(); } private void init() { //从activity_register.xml 页面中获取对应的UI控件 Button btn_register = (Button) findViewById(R.id.btn_register); et_user_name= (EditText) findViewById(R.id.et_user_name); et_psw= (EditText) findViewById(R.id.et_psw); et_psw_again= (EditText) findViewById(R.id.et_psw_again); Sex= (RadioGroup) findViewById(R.id.SexRadio); //注册按钮 btn_register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取输入在相应控件中的字符串 getEditString(); //判断输入框内容 int sex; int sexChoseId = Sex.getCheckedRadioButtonId(); switch (sexChoseId) { case R.id.mainRegisterRdBtnFemale: sex = 0; break; case R.id.mainRegisterRdBtnMale: sex = 1; break; default: sex = -1; break; } if(TextUtils.isEmpty(userName)){ Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show(); }else if(TextUtils.isEmpty(psw)){ Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show(); }else if(TextUtils.isEmpty(pswAgain)) { Toast.makeText(RegisterActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show(); } else if (sex<0){ Toast.makeText(RegisterActivity.this, "请选择性别", Toast.LENGTH_SHORT).show(); }else if(!psw.equals(pswAgain)){ Toast.makeText(RegisterActivity.this, "输入两次的密码不一样", Toast.LENGTH_SHORT).show(); /** *从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名 */ }else if(isExistUserName(userName)){ Toast.makeText(RegisterActivity.this, "此账户名已经存在", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); //把账号、密码和账号标识保存到sp里面 /** * 保存账号和密码到SharedPreferences中 */ saveRegisterInfo(userName, psw); //注册成功后把账号传递到LoginActivity.java中 // 返回值到loginActivity显示 Intent data = new Intent(); data.putExtra("userName", userName); setResult(RESULT_OK, data); //RESULT_OK为Activity系统常量,状态码为-1, // 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值 RegisterActivity.this.finish(); } } }); } /** * 获取控件中的字符串 */ private void getEditString(){ userName=et_user_name.getText().toString().trim(); psw=et_psw.getText().toString().trim(); pswAgain=et_psw_again.getText().toString().trim(); } /** * 从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名 */ private boolean isExistUserName(String userName){ boolean has_userName=false; //mode_private SharedPreferences sp = getSharedPreferences( ); // "loginInfo", MODE_PRIVATE SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE); //获取密码 String spPsw=sp.getString(userName, "");//传入用户名获取密码 //如果密码不为空则确实保存过这个用户名 if(!TextUtils.isEmpty(spPsw)) { has_userName=true; } return has_userName; } /** * 保存账号和密码到SharedPreferences中SharedPreferences */ private void saveRegisterInfo(String userName,String psw){ String md5Psw = MD5Utils.md5(psw);//把密码用MD5加密 //loginInfo表示文件名, mode_private SharedPreferences sp = getSharedPreferences( ); SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE); //获取编辑器, SharedPreferences.Editor editor -> sp.edit(); SharedPreferences.Editor editor=sp.edit(); //以用户名为key,密码为value保存在SharedPreferences中 //key,value,如键值对,editor.putString(用户名,密码); editor.putString(userName, md5Psw); //提交修改 editor.commit(); editor.apply(); }}
最后是找回密码界面 activity_lost_find.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/colorPrimary" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="忘记密码" android:textSize="30sp"/> LinearLayout> <LinearLayout android:layout_marginTop="15dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:paddingTop="2dp" android:paddingBottom="2dp" android:paddingLeft="6dp" android:hint="请输入忘记密码的账号" android:singleLine="true" android:maxLength="15" android:background="#ffffff" android:textSize="24sp" /> LinearLayout>LinearLayout>
找回密码功能只是新建了一个活动,并未实现功能。
小结:功能未使用内置数据库SQLite,运用MD5加密算法完成。
更多相关文章
- 一个简单的Android破解WIFI密码的方法(实用)
- Android实现登录界面和功能实例
- android即时通讯开发笔记(一)绪论~实现用户登录,自动登录,注销功能
- Android之微信第三方登录
- Android(安卓)webview 设置cookie 保持登录状态
- 菜鸟的安卓实习之路----关于EditText的最大长度maxLength和密码
- Android实现登录功能,Android与服务器数据交互,使用tomcat、mysql
- Android---网络交互之登录嘀咕网
- App基于html/css/js的开发