ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-12-14 21:59:56  阅读:162  来源: 互联网

标签:Toast account 用户名 builder studio 为空 Android password 共享


 

 

//注册功能
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>

用户注册信息:

 

标签:Toast,account,用户名,builder,studio,为空,Android,password,共享
来源: https://blog.csdn.net/weixin_45565469/article/details/121940645

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有