Android:实现一个CaseLogin数据存储

这个的任务是用SharedPreferences进行存储数据的操作

当用户选中了记住密码复选框,并成功登录一次之后,这个时候如果再重新启动登录界面,之前输入的用户名和密码就会显示在文本框中。

具体的图片如下:

具体代码如下:

LoginActivity

package cn.edu.bzu.case_login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private CheckBox CBPass;
    private EditText Name;
    private EditText Password;

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

//存
    public void save(View view) {
        Name = (EditText) findViewById(R.id.etName);
        Password = (EditText) findViewById(R.id.etPassword);
        CBPass = (CheckBox) findViewById(R.id.cbPass);

        String name = Name.getText().toString();
        String password = Password.getText().toString();
        Boolean cbpass = CBPass.isChecked();

  //正确的用户名和密码
        if (name.equals("admin") && password.equals("123456")) {  

            SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("etName", name);
            editor.putString("etPassword", password);
            editor.putBoolean("Click", cbpass);
            editor.commit();
            Intent intent = new Intent(LoginActivity.this, WelActivity.class);
            startActivity(intent);
        } else {
            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }

//读
    public void read() {
        Name = (EditText) findViewById(R.id.etName);
        Password = (EditText) findViewById(R.id.etPassword);
        CBPass = (CheckBox) findViewById(R.id.cbPass);
        SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
        String name = sp.getString("etName", "");
        String password = sp.getString("etPassword", "");
        boolean cbpass = sp.getBoolean("Click", false);

//判断是否选中保存密码按钮
        if (cbpass == true) {
            Password.setText(password);
            Name.setText(name);
            CBPass.setChecked(cbpass);

        } else {
            Password.setText("");
            Name.setText(name);
            CBPass.setChecked(cbpass);
        }
    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74


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

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

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值