Android studio 利用共享存储进行用户的注册和登录验证功能

 

//注册功能
public class MainActivity extends AppCompatActivity {

    //声明共享存储(全局变量)
    private SharedPreferences spf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //在打开页面时初始化共享存储对象spf  "users"表名
        spf=getSharedPreferences("users", Context.MODE_PRIVATE);
    }

    /**
     * 注册
     * key : value
     * @param view
     */
    public void register(View view){
        //获取页面视图组件
        EditText accountEt = findViewById(R.id.account);
        EditText passwordEt = findViewById(R.id.password);
        EditText repwdEt = findViewById(R.id.repwd);

        //获取用户名和密码
        String account =accountEt.getText().toString();
        String password =passwordEt.getText().toString();
        String repwd=repwdEt.getText().toString();

        //表单验证
        //判断用户名是否为空
        if (account!=null && !"".equals(account)){
            //用户名不为空
            //比较输入的用户名是否已经被注册存在
            if (account.equals(spf.getString("account",""))){
                //用户名已存在
                //Toast.makeText(MainActivity.this, "该用户名已存在!", Toast.LENGTH_SHORT).show();
                showDialog("该用户名已经存在");
                return;//终止方法执行
            }
        }else{
            //用户名为空
            //Toast方法适用于不严重的提醒情况 content:上下文 text:提示的信息内容
            //Toast.makeText(MainActivity.this, "用户姓名不能为空!", Toast.LENGTH_SHORT).show();
            showDialog("用户名不能为空!");
            return;//终止方法执行
        }
        //密码验证
        //判断密码是否为空
        if (password==null || "".equals(password)){
            //判断密码不能为空
            //Toast.makeText(MainActivity.this, "密码不能为空!", Toast.LENGTH_SHORT).show();
            showDialog("密码不能为空");
            return;
        }
        //验证两次密码是否相同
        if (!password.equals(repwd)){
            //Toast.makeText(MainActivity.this, "两次密码不一致!", Toast.LENGTH_SHORT).show();
            showDialog("两次密码不一致");
            return;
        }

        //保存用户名和密码
        SharedPreferences.Editor editor=spf.edit();
        editor.putString("account",account);//账号名
        editor.putString("password",password);//密码
        editor.apply();//提交数据
        Toast.makeText(MainActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();

        //跳转到登录页面
        Intent intent=new Intent(MainActivity.this,LoginActivity.class);
        startActivity(intent);
    }

    //设置提示框
    public void showDialog(String msg){
        //1、创建AlertDialog.Builder对象
        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
        //2、设置提示窗口相关信息
        builder.setTitle("提示");
        builder.setMessage(msg);//提示信息
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.setCancelable(false);//点击空白区域不能被关掉  true能被关掉
        builder.show();//显示提示框
    }
}
//注册页面布局
<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=".MainActivity"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        android:gravity="center_horizontal"
        android:textSize="50sp"/>

    <EditText
        android:id="@+id/account"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入账号名"
        android:textSize="20sp"/>

    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入密码"
        android:textSize="20sp"/>

    <EditText
        android:id="@+id/repwd"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请确认密码"
        android:textSize="20sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="确认注册"
        android:textSize="30sp"
        android:layout_marginTop="20dp"
        android:onClick="register"/>

</LinearLayout>
//登录页面功能
public class LoginActivity extends AppCompatActivity {

    //声明共享存储(全局变量)
    private SharedPreferences spf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //在打开页面时初始化共享存储对象spf  "users"表名
        spf=getSharedPreferences("users", Context.MODE_PRIVATE);
    }

    /**
     * 登录
     * @param view
     */
    public void login(View view){
        //获取页面视图组件
        EditText accountEt=findViewById(R.id.account);
        EditText passwordEt=findViewById(R.id.password);

        //获取用户名
        String account=accountEt.getText().toString();
        String password=passwordEt.getText().toString();

        //表单验证
        //判断用户名是否为空
        if (account==null || "".equals(account)){
            showDialog("用户名不能为空!");
            return;
        }
        //判断密码是否为空
        if (password==null || "".equals(password)){
            showDialog("密码不能为空!");
            return;
        }
        //验证登录,将用户输入的用户名和密码和共享存储里面的内容进行比对
        if (account.equals(spf.getString("account",""))&&
                password.equals(spf.getString("password",""))){
            showDialog("登录成功!");
            //登录成功后跳转到首页
            Intent intent=new Intent(LoginActivity.this,HomeActivity.class);
            //传递登录成功的用户名
            intent.putExtra("account",account);
            startActivity(intent);
        }else{
            showDialog("用户名或密码输入错误!");
        }
    }

    //设置提示框
    public void showDialog(String msg){
        //1、创建AlertDialog.Builder对象
        AlertDialog.Builder builder=new AlertDialog.Builder(LoginActivity.this);
        //2、设置提示窗口相关信息
        builder.setTitle("提示");
        builder.setMessage(msg);//提示信息
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.setCancelable(false);//点击空白区域不能被关掉  true能被关掉
        builder.show();//显示提示框
    }
}
//登录页面布局
<RelativeLayout 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=".LoginActivity"
    android:padding="20dp">

    <TextView
        android:id="@+id/register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="40sp"
        android:gravity="center_horizontal"/>

    <EditText
        android:id="@+id/account"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入账号名"
        android:layout_below="@id/register"
        android:textSize="20sp"/>

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入密码"
        android:textSize="20sp"
        android:layout_below="@id/account"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="确认登录"
        android:textSize="30sp"
        android:layout_marginTop="20dp"
        android:layout_below="@id/password"
        android:onClick="login"/>

</RelativeLayout>
//首页显示欢迎信息
public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //获取意图
        Intent intent=getIntent();
        String account=intent.getStringExtra("account");
        //页面上显示传递的内容
        //设置欢迎信息
        TextView tv=findViewById(R.id.welcomMessage);
        tv.setText("欢迎"+account+"登录本系统!");
    }
}
//首页布局
<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=".HomeActivity"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:id="@+id/welcomMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="35dp"
        android:gravity="center_horizontal"
        android:textColor="#99CCFF"/>
</LinearLayout>

用户注册信息:

到此这篇关于Android studio 利用共享存储进行用户的注册和登录验证的文章就介绍到这了,更多相关Android studio 注册和登录验证内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Android studio实现app登录界面

    Android studio设计app登录界面UI界面设计在设计登录界面时,可以使用不同布局方式来实现该功能,通常情况下使用的是LinearLayout(线性布局)和TableLayout(表格布局),在本文中使用线性布局.登陆界面需要几项必不可少的控件,如下所示: TextView:用于显示标题和“用户名"和"密码"的提示;标题设置 <TextView        android:layout_width="wrap_content"     

  • Android Studio连接MySql实现登录注册(附源代码)

    本文主要介绍了Android Studio连接MySql实现登录注册,分享给大家,具体如下: 一.创建工程 1.创建一个空白工程 2.随便起一个名称 3.设置网络连接权限 <uses-permission android:name="android.permission.INTERNET"/> 二.引入Mysql驱动包 1.切换到普通Java工程 2.在libs当中引入MySQL的jar包 将mysql的驱动包复制到libs当中 三.编写数据库和dao以及JDBC相关代码

  • Android Studio实现登录功能案例讲解

    LoginActivity.java 包 com.hyx.example.mymap; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Ha

  • Android Studio 通过登录功能介绍SQLite数据库的使用流程

    前言: SQLite简介:是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了.它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl.C#.PHP.Java等,还有ODBC接口,同样比起Mysql.PostgreSQL这两款开源的世

  • Android Studio实现QQ的注册登录和好友列表跳转

    一.项目概述 本次项目主要包含了注册.登录和好友列表三个界面以及之间相互跳转.其中好友列表界面设计的很详细,有好友头像和消息内容.用户先点击注册按钮进入注册界面,输入完账号和密码后,点击注册,跳转到登录界面,这时候账号和密码也被传了过来,点击登录按钮进入好友列表界面,这时候用户名也被传递过来. 二.开发环境 三.详细设计 1.登录界面的搭建 整体布局是相对布局RelativeLayout,上来ImageView是头像框,下面一个LinearLayout,显示的账号TextView和EditTex

  • Android Studio连接SQLite数据库的登录注册实现

    1.先看一下项目目录: 2.新建一个AS项目,创建如上图所示的目录结构,然后添加内容: (1)修改添加布局文件: activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android&quo

  • Android Studio实现简易登录界面制作

    想要制作一个简易的登录界面非常容易,总体上来说就是UI布局.给定id.新建跳转的页面.以及输入账号密码的获取与判断,那么接下来就开始制作吧! 1.首先就是Activity中的组件布局,这个就不一一列举了!自己把两个EditText和一个Button摆好就ok了,像按钮的点击效果可以自己设计一下(默认状态是什么颜色,按下去是什么颜色). 2.再一个就是要给定控件一个id <?xml version="1.0" encoding="utf-8"?> <

  • Android Studio+Servlet+MySql实现登录注册

    一.Android 项目当中设置明文传输 1.设置明文传输的xml <?xml version="1.0" encoding="UTF-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true"/> </network-security-config> 2.引入上述创建的xml android:networ

  • Android Studio实现登录界面功能

    本文实例为大家分享了vue + element ui实现锚点定位的具体代码,供大家参考,具体内容如下 题目 设计一个登录界面.要求: a) 包含用户名.密码.记住密码.“忘记密码”按钮和“登录”按钮.b) 单击“忘记密码”按钮弹出提示对话框,对话框内容自拟. 答案 activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app=&q

  • Android Studio实现简单的QQ登录界面的示例代码

    一.项目概述 QQ是我们日常生活使用最多的软件之一,包含登录界面和进入后的聊天界面.好友列表界面和空间动态界面等.登录界面的制作比较简单,主要考验布局的使用,是实现QQ项目的第一步.现在APP开发的首要工作都是实现登录页面,所以学会了QQ登录界面对以后的软件开发有着很重要的作用. 二.开发环境 三.详细设计 1.头像设计 首先在layout文件里面选择了RelativeLayout(相对布局)作为整个页面的布局. 在顶端放置了一个ImageView控件,宽度和高度设置的都是70dp,水平居中设置

随机推荐