上次完成了登录界面的实现,今天分享一下注册界面的布局以及代码和MD5的加密。
一、首先创建一个Activity,命名为SecondActivity,当然名字随意命名,我比较懒所以我的基本都是first,second…然后设计activity_second.xml布局文件,注册界面如下:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity"
android:orientation="vertical"
android:background="@drawable/nice"><TextView
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="欢迎来到注册界面!"
android:textSize="30dp"
android:gravity="center"
android:textColor="@color/forestgreen"/><LinearLayout
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"><EditText
android:id="@+id/user"
android:gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/sharp"
android:hint="@string/tip_account"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
android:textSize="20sp"
android:digits="0123456789"/><EditText
android:layout_marginTop="20dp"
android:id="@+id/password"
android:gravity="center"
android:background="@drawable/sharp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/tip_password"
android:inputType="textPassword"
android:textSize="20sp"
android:password="true"/><EditText
android:layout_marginTop="20dp"
android:id="@+id/password1"
android:gravity="center"
android:background="@drawable/sharp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请再次输入密码"
android:inputType="textPassword"
android:textSize="20sp"
android:password="true"/><Button
android:id="@+id/btn_register"
android:layout_marginTop="20dp"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:textColor="@color/white"
android:layout_below="@id/password_input"
android:layout_centerHorizontal="true"
android:background="@color/mediumseagreen"/></LinearLayout></LinearLayout>
图片以及字体颜色什么的可以根据自己爱好修改
二、接下来就是SecondActivity.java注册界面的代码:
package com.example.myapplication;import android.content.Intent;import android.content.SharedPreferences;import android.content.pm.ActivityInfo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import javax.xml.transform.Templates;publicclassSecondActivityextendsAppCompatActivity{private Button btn_register;//注册按钮//用户名,密码,再次输入的密码的控件private EditText user,password,password1;//用户名,密码,再次输入的密码的控件的获取值private String userName,psw,pswAgain;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//设置页面布局 ,注册界面setContentView(R.layout.activity_second);//设置此界面为竖屏setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);init();}privatevoidinit(){
btn_register=findViewById(R.id.btn_register);
user=findViewById(R.id.user);
password=findViewById(R.id.password);
password1=findViewById(R.id.password1);//注册按钮
btn_register.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(View v){//获取输入在相应控件中的字符串getEditString();//判断输入框内容if(TextUtils.isEmpty(userName)){
Toast.makeText(SecondActivity.this,"请输入用户名", Toast.LENGTH_SHORT).show();return;}elseif(TextUtils.isEmpty(psw)){
Toast.makeText(SecondActivity.this,"请输入密码", Toast.LENGTH_SHORT).show();return;}elseif(TextUtils.isEmpty(pswAgain)){
Toast.makeText(SecondActivity.this,"请再次输入密码", Toast.LENGTH_SHORT).show();return;}elseif(!psw.equals(pswAgain)){
Toast.makeText(SecondActivity.this,"输入两次的密码不一样", Toast.LENGTH_SHORT).show();return;/**
*从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名
*/}elseif(isExistUserName(userName)){
Toast.makeText(SecondActivity.this,"此账户名已经存在", Toast.LENGTH_SHORT).show();return;}else{
Toast.makeText(SecondActivity.this,"注册成功", Toast.LENGTH_SHORT).show();//把账号、密码和账号标识保存到sp里面/**
* 保存账号和密码到SharedPreferences中
*/saveRegisterInfo(userName, psw);//注册成功后把账号传递到LoginActivity.java中// 返回值到loginActivity显示
Intent data=newIntent();
data.putExtra("userName", userName);setResult(RESULT_OK, data);//RESULT_OK为Activity系统常量,状态码为-1,// 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值
SecondActivity.this.finish();}}});}/**
* 获取控件中的字符串
*/privatevoidgetEditString(){
userName=user.getText().toString().trim();
psw=password.getText().toString().trim();
pswAgain=password1.getText().toString().trim();}/**
* 从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名
*/privatebooleanisExistUserName(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
*/privatevoidsaveRegisterInfo(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.commit();}}
三、还有很重要的一步,MD5用于确保信息传输完整一致。实现密码的加密,因为注册需要涉及密码,这个时候我们就需要用MD5进行加密。具体代码如下:
MD5Utils.java
package com.example.myapplication;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;publicclassMD5Utils{publicstatic Stringmd5(String text){
MessageDigest digest= null;try{
digest= MessageDigest.getInstance("md5");// 数组 byte[] result -> digest.digest( ); 文本 text.getBytes();byte[] result= digest.digest(text.getBytes());//创建StringBuilder对象 然后建议StringBuffer,安全性高//StringBuilder sb = new StringBuilder();
StringBuffer sb=newStringBuffer();// result数组,digest.digest ( ); -> text.getBytes();// for 循环数组byte[] result;for(byte b: result){int number= b&0xff;
String hex= Integer.toHexString(number);if(hex.length()==1){
sb.append("0"+ hex);}else{
sb.append(hex);}}//sb StringBuffer sb = new StringBuffer();对象实例化return sb.toString();}catch(NoSuchAlgorithmException e){
e.printStackTrace();//发送异常return空字符串return"";}}}