Androidstudio安卓开发,SharedPreferences实现登录注册

1. 项目涉及到的技术点

  1. SharedPreferences的使用

2. 效果图

注册

登录

3. 实现过程

  1. 注册布局文件:activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
    android:orientation="vertical"
    tools:context=".LoginActivity">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/img_login_bg" />


        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
            app:title="注册"
            app:titleTextColor="@color/white" />
    </RelativeLayout>


    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="-10dp"
        android:background="@drawable/img_shape_login_bg"
        android:orientation="vertical">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="30dp"
            android:layout_marginRight="30dp"
            android:orientation="vertical">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="欢迎注册"
                android:textColor="#FF7F00"
                android:textSize="24sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="注册您的账号!"
                android:textColor="#BDBDBD"
                android:textSize="13sp" />


            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="46dp"
                    android:text="用户名"
                    android:textColor="#585858" />

                <EditText
                    android:id="@+id/et_username"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginTop="6dp"
                    android:background="@drawable/login_et_bg"
                    android:hint="请输入用户名"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:textSize="14sp" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="16dp"
                    android:text="密码"
                    android:textColor="#585858" />

                <EditText
                    android:id="@+id/et_password"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginTop="6dp"
                    android:background="@drawable/login_et_bg"
                    android:hint="请输入密码"
                    android:inputType="textPassword"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:textSize="14sp" />


                <Button
                    android:id="@+id/register"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/login_et_bg"
                    android:text="注册" />


            </androidx.appcompat.widget.LinearLayoutCompat>

        </androidx.appcompat.widget.LinearLayoutCompat>

    </androidx.appcompat.widget.LinearLayoutCompat>


</androidx.appcompat.widget.LinearLayoutCompat>
  1. 注册页具体代码:RegisterActivity
public class RegisterActivity extends AppCompatActivity {

    private EditText et_username;
    private EditText et_password;
    private SharedPreferences mSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //获取mSharedPreferences
        mSharedPreferences = getSharedPreferences("user", MODE_PRIVATE);

        //初始化控件
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);

        //注册
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(RegisterActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                } else {
                    SharedPreferences.Editor edit = mSharedPreferences.edit();
                    edit.putString("username", username);
                    edit.putString("password", password);
                    //一定要提交
                    edit.commit();
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();

                    finish();
                }
            }
        });

        //返回
        findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}

  1. 登录布局文件:activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
    android:orientation="vertical"
    tools:context=".LoginActivity">


    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="180dp"
                    android:scaleType="centerCrop"
                    android:src="@mipmap/img_login_bg" />


                <androidx.appcompat.widget.Toolbar
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:title="登录"
                    app:titleTextColor="@color/white" />
            </RelativeLayout>


            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="-10dp"
                android:background="@drawable/img_shape_login_bg"
                android:orientation="vertical">

                <androidx.appcompat.widget.LinearLayoutCompat
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="30dp"
                    android:orientation="vertical">


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="欢迎回来"
                        android:textColor="#FF7F00"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:text="继续登录您的账号!"
                        android:textColor="#BDBDBD"
                        android:textSize="13sp" />


                    <androidx.appcompat.widget.LinearLayoutCompat
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">


                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="46dp"
                            android:text="用户名"
                            android:textColor="#585858" />

                        <EditText
                            android:id="@+id/et_username"
                            android:layout_width="match_parent"
                            android:layout_height="40dp"
                            android:layout_marginTop="6dp"
                            android:background="@drawable/login_et_bg"
                            android:hint="请输入用户名"
                            android:paddingLeft="10dp"
                            android:paddingRight="10dp"
                            android:textSize="14sp" />


                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="16dp"
                            android:text="密码"
                            android:textColor="#585858" />

                        <EditText
                            android:id="@+id/et_password"
                            android:layout_width="match_parent"
                            android:layout_height="40dp"
                            android:layout_marginTop="6dp"
                            android:background="@drawable/login_et_bg"
                            android:hint="请输入密码"
                            android:inputType="textPassword"
                            android:paddingLeft="10dp"
                            android:paddingRight="10dp"
                            android:textSize="14sp" />


                        <CheckBox
                            android:id="@+id/checkbox"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="记住密码" />

                        <Button
                            android:id="@+id/login"
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            android:layout_marginTop="20dp"
                            android:background="@drawable/login_et_bg"
                            android:text="登录" />


                        <TextView
                            android:id="@+id/register"
                            android:layout_width="wrap_content"
                            android:layout_height="30dp"
                            android:layout_gravity="center"
                            android:layout_marginTop="40dp"
                            android:gravity="center"
                            android:text="还没有账号? 注册"
                            android:textColor="#747481" />

                    </androidx.appcompat.widget.LinearLayoutCompat>

                </androidx.appcompat.widget.LinearLayoutCompat>

            </androidx.appcompat.widget.LinearLayoutCompat>

        </androidx.appcompat.widget.LinearLayoutCompat>
    </androidx.core.widget.NestedScrollView>


</androidx.appcompat.widget.LinearLayoutCompat>
  1. 登录页具体代码:LoginActivity
public class LoginActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    private SharedPreferences mSharedPreferences;

    private CheckBox checkbox;
    private boolean is_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //获取mSharedPreferences
        mSharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
        //初始化控件
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        checkbox = findViewById(R.id.checkbox);

        //判断是否记住了密码
        is_login = mSharedPreferences.getBoolean("is_login", false);
        if (is_login) {
            et_username.setText(mSharedPreferences.getString("username", null));
            et_password.setText(mSharedPreferences.getString("password", null));
            checkbox.setChecked(true);
        }


        //注册
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到注册页面
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
        //登录
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                } else {
                    String name = mSharedPreferences.getString("username", null);
                    String pwd = mSharedPreferences.getString("password", null);
                    if (username.equals(name) && password.equals(pwd)) {
                        //保存登录状态
                        SharedPreferences.Editor editor = mSharedPreferences.edit();
                        editor.putBoolean("is_login", is_login);
                        editor.putString("username", username);
                        editor.putString("password", password);
                        editor.apply();
                        //跳转到主页面
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(LoginActivity.this, NewsMainActivity.class));
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                is_login = b;
            }
        });
    }
}

xml中所设计的图片资源,请自行替换即可

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值