使用SharedPreferences来保存用户登录帐号密码

来源:互联网 发布:debian centos 读音 编辑:程序博客网 时间:2024/04/24 22:02

前面我们使用过『往内部存储里写文件』的方式来保存用户登录的帐号和密码。
其实要保存用户帐号密码,更好的方式是使用SharedPreferences

    /**     * 登录操作     * @param v View对象     */    public void login(View v){        //获取输入的用户名和密码        EditText et_name = (EditText)findViewById(R.id.et_name);        EditText et_pwd = (EditText)findViewById(R.id.et_pwd);        String name = et_name.getText().toString();        String pwd = et_pwd.getText().toString();        //判断有无勾选『记住密码』        CheckBox box = (CheckBox)findViewById(R.id.Rememb);        if (box.isChecked()){           //使用SharedPreferences来保存用户名和密码            SharedPreferences sp = getSharedPreferences("info",MODE_PRIVATE);            SharedPreferences.Editor ed = sp.edit();            ed.putString("name",name);            ed.putString("pwd",pwd);            ed.commit();        }        //Toast对话框提示        Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();    }
0 0
原创粉丝点击