AndroidStudio实现用户登录注册界面代码(二)

上次完成了登录界面的实现,今天分享一下注册界面的布局以及代码和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;

public class SecondActivity extends AppCompatActivity {
    private Button btn_register;//注册按钮
    //用户名,密码,再次输入的密码的控件
    private EditText user,password,password1;
    //用户名,密码,再次输入的密码的控件的获取值
    private String userName,psw,pswAgain;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //设置页面布局 ,注册界面
        setContentView(R.layout.activity_second);
        //设置此界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
    }

    private void init() {

        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(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //获取输入在相应控件中的字符串
                getEditString();
                //判断输入框内容
                if(TextUtils.isEmpty(userName)){
                    Toast.makeText(SecondActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(psw)){
                    Toast.makeText(SecondActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(pswAgain)){
                    Toast.makeText(SecondActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(!psw.equals(pswAgain)){
                    Toast.makeText(SecondActivity.this, "输入两次的密码不一样", Toast.LENGTH_SHORT).show();
                    return;
                    /**
                     *从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名
                     */
                }else if(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 = new Intent();
                    data.putExtra("userName", userName);
                    setResult(RESULT_OK, data);
                    //RESULT_OK为Activity系统常量,状态码为-1,
                    // 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值
                    SecondActivity.this.finish();
                }
            }
        });
    }
    /**
     * 获取控件中的字符串
     */
    private void getEditString(){
        userName=user.getText().toString().trim();
        psw=password.getText().toString().trim();
        pswAgain=password1.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.commit();
    }
    }
三、还有很重要的一步,MD5用于确保信息传输完整一致。实现密码的加密,因为注册需要涉及密码,这个时候我们就需要用MD5进行加密。具体代码如下:

MD5Utils.java

package com.example.myapplication;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    public static String md5(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 = new StringBuffer();
            // 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 "";
        }
    }
}
四、下面是一些代码截图:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

最后说明一下:运行主界面——点击注册(可以看到密码是不可见的)注册完毕点击“注册”按钮,会弹出“注册成功”用的是信息提示方式“Toast”然后自动跳转到主界面,这样注册就完成了。点击登录按钮,会进入登录界面,输入注册的用户名和密码,点击登录则进入下一界面,登陆成功。

源码下载:MyApplication.zip
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值