100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Android仿QQ登录界面示例 实现登录 注册功能。

Android仿QQ登录界面示例 实现登录 注册功能。

时间:2019-02-10 18:13:41

相关推荐

Android仿QQ登录界面示例 实现登录 注册功能。

首语

欢迎大家关注我的公众号:八归少年

微信公众号优先更新文章。扫描上面二维码即可关注!一起进步,一同成长。

Android开发经常用到注册、登录功能,于是便整理出一般通用的登录界面,并实现其相应功能。供读者参阅。此项目包含三个活动,即登录,注册界面,找回密码。

GitHub源码地址:LoginTest

界面

下面是对代码的分析过程。

首先是登录界面 activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><!--登录界面,用LinearLayout--><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@mipmap/bg"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="35dp"><ImageViewandroid:id="@+id/symbol"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid: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><!--输入框--><EditTextandroid: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" /><!--输入框--><EditTextandroid: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"/><!--按钮--><Buttonandroid: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"/><RelativeLayoutandroid:layout_marginTop="15dp"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/tv_register"android:layout_alignParentRight="true"android:layout_marginRight="20dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="新用户注册"/><!--layout_weight="1" layout_width="0dp"实现均分效果--><TextViewandroid: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><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><RelativeLayoutandroid:layout_gravity="center"android:layout_marginBottom="30dp"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid: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;//编辑框@Overrideprotected 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() {@Overridepublic void onClick(View v) {//为了跳转到注册界面,并实现注册功能Intent intent=new Intent(MainActivity.this,RegisterActivity.class);startActivityForResult(intent, 1);}});//找回密码控件的点击事件tv_find_psw.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(MainActivity.this,LostFindActivity.class));}});//登录按钮的点击事件btn_login.setOnClickListener(new View.OnClickListener() {@Overridepublic 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.isEmptyif (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"?><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:background="@color/colorPrimary"android:gravity="center_vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="注册"android:textSize="30sp"/></RelativeLayout><LinearLayoutandroid: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" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:gravity="center_vertical"android:orientation="horizontal" ><EditTextandroid: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><LinearLayoutandroid:background="@drawable/shap"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center_vertical"android:orientation="horizontal" ><EditTextandroid: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><LinearLayoutandroid:background="@drawable/shap"android:layout_width="match_parent"android:layout_height="50dp"android:gravity="center_vertical"android:orientation="horizontal" ><EditTextandroid: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><LinearLayoutandroid:background="@drawable/shap"android:layout_width="match_parent"android:layout_height="60dp"android:gravity="center_vertical"android:orientation="horizontal" ><TextViewandroid: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" /><RadioGroupandroid:layout_width="0px"android:layout_height="wrap_content"android:layout_weight="2.6"android:id="@+id/SexRadio"android:paddingLeft="5dp"><RadioButtonandroid:id="@+id/mainRegisterRdBtnFemale"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女生"/><RadioButtonandroid:id="@+id/mainRegisterRdBtnMale"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男生"/></RadioGroup></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:gravity="center_vertical"android:orientation="horizontal" ><EditTextandroid: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><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp" ><Buttonandroid: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;@Overrideprotected 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() {@Overridepublic 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_PRIVATESharedPreferences 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);//提交修改 mit();editor.apply();}}

最后是找回密码界面 activity_lost_find.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:background="@color/colorPrimary"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="忘记密码"android:textSize="30sp"/></LinearLayout><LinearLayoutandroid:layout_marginTop="15dp"android:layout_width="match_parent"android:layout_height="wrap_content"><EditTextandroid: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加密算法完成。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。