zoukankan      html  css  js  c++  java
  • Android SharedPreferences 存储用户登录密码

    SharedPreferences 存储是在手机上的名字对存储,以XML 文件的形式。

    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.widget.EditText;

    public class DBSharedPreferences extends Activity {
        public static final String SETTING_INFOS = "SETTING_Infos"; //这个就是XML文件名
        public static final String NAME = "NAME";
        public static final String PASSWORD = "PASSWORD";
       
        private EditText field_name;
        private EditText filed_pass;


        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //Find VIew
            field_name = (EditText) findViewById(R.id.name);
            filed_pass = (EditText) findViewById(R.id.password);
            // Restore preferences
            SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0); //获取SharedPreferences 对象
            String name = settings.getString(NAME, ""); //读内容
            String password = settings.getString(PASSWORD, "");
            //Set value
            field_name.setText(name);
            filed_pass.setText(password);

        }
       

    //重载关闭事件
        @Override
        protected void onStop(){
            super.onStop();
            SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0);
            settings.edit()
                .putString(NAME, field_name.getText().toString()) //写内容
                .putString(PASSWORD, filed_pass.getText().toString())
                .commit();
        }
    }

  • 相关阅读:
    [转载]--python3.6 错误: ModuleNotFoundError:No module named "Crypto"
    [笔记]--RedHat6.5使用CentOS的yum源
    [笔记]--Linux公社,想要的都在里面
    [笔记]--vsftpd配置教程
    Vue 中 axios 配置使用
    Element-ui自定义主题换肤
    vue-cli项目打包需要修改的路径问题
    cookie和session 以及 localStorage和sessionStorage之间的区别和应用场景
    正则表达式
    详解-vue项目中的文件和目录
  • 原文地址:https://www.cnblogs.com/finehappy/p/2048933.html
Copyright © 2011-2022 走看看