SharedPreferences的详解

SharedPreferences的详解

  • 困扰了很久…简单的模仿下
    如下效果
    在这里插入图片描述
    首先考虑自己的按钮需求
    <1>记住密码之后,再次进入app用户名与密码已显示;
    <2>直接登录,进入app直接登录
    <3>点击登录按钮时,(什么也不选),下次进入,什么也没有.
    <4>只有点击登录时,我才开始用sp对象保存密码[起步要求];
    那我们保存什么呢?一定是username与userpassword以及两个复选框的状态要求;
    <5>在onCreate()主方法中我们直接获取两种状态,然后对此进行前三点的逻辑判断.

  • 其实想起来还是比较简单的

  • 首先 点击按钮的操作

 case R.id.but_login:
                mb = this.mobile.getText().toString().trim();
                pwd = this.password.getText().toString().trim();
                if (mb.equals("") || pwd.equals("")) {
                    Toast.makeText(MainActivity.this, "不可以为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                SharedPreferences.Editor edit1 = sp.edit();
                //记住密码勾选中
                if (cbRePwd.isChecked()){
                    edit1.putString("name", mb);
                    edit1.putString("pwd", pwd);
                    edit1.putBoolean("boolrepwd",true);
                }
                //直接登录勾选中
                if (cbLogin.isChecked()){
                    edit1.putBoolean("boollogin",true);
                }
                //一定要记着最后关闭.
                edit1.commit();
               /* loginPresentle = new LoginPresentle(this);
                loginPresentle.login(mb, pwd);*/
                break;
  • 其次 onCreate()方法中的具体操作
       //直接获取状态<两个checkBox中的状态>
        boolean boolrepwd = sp.getBoolean("boolrepwd", false);
        boolean boollogin = sp.getBoolean("boollogin", false);
        //记住密码选中
        if (boolrepwd){
            //取值
            String sp_name = sp.getString("name", mb);
            String sp_pwd = sp.getString("pwd", this.pwd);
            //设值
            mobile.setText(sp_name);
            password.setText(sp_pwd);
            //已经实现记住密码再次进入显示用户名密码
        }
        //刷新状态cbRePwd
        cbRePwd.setChecked(boolrepwd);
        //直接登录选中----->那我跳转就好了
        if (boollogin){
            startActivity(new Intent(MainActivity.this,HomeActivity.class));
            finish();
        }
        //记住密码的点击
        cbRePwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (! isChecked){//如果取消勾选记住密码,自动登录也需要取消
                    cbLogin.setChecked(false);
                }else{
                   //剔除我的值
                    sp.edit().remove("name");
                    sp.edit().remove("pwd");
                }
                sp.edit().putBoolean("boolrepwd",cbRePwd.isChecked()).commit();
            }
        });
        //直接登录的点击
        cbLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    cbRePwd.setChecked(true);
                }
                sp.edit().putBoolean("boollogin",cbLogin.isChecked()).commit();
            }
        });
  • 布局
<ImageView
        android:layout_width="100dp"
        android:layout_marginTop="100dp"
        android:layout_height="100dp"
        android:src="@drawable/user"
        android:layout_gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="用户名"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="密码"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/cb_login"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="直接登录"/>
        <CheckBox
            android:id="@+id/cb_re_pwd"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="记住密码"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/but_login"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="登录"/>
        <Button
            android:id="@+id/but_add"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="注册"/>
    </LinearLayout>
  • 后序
    撤销用户以及退出登录…
    日后更新…
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值