• Android 共享参数 SharedPreferences


    完成共享参数的读写

    public class SharedPreference {
    
        private Context context;
    
        public SharedPreference(Context context) {
            // TODO Auto-generated constructor stub
    
            this.context = context;
        }
    
        public boolean saveMessage(String name, String passwd) {
    
            boolean flag = false;
    
            // 自动保存成 userinfo.xml
            SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    
            // 对数据进行编辑
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("name", name);
            editor.putString("passwd", passwd);
            // 将数据持久化到存储介质中
            flag = editor.commit();
            return flag;
        }
    
        public Map<String, Object> getMessage() {
            Map<String, Object> map = new HashMap<String, Object>();
            SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    
            String name = sharedPreferences.getString("name", "");
            String passwd = sharedPreferences.getString("passwd", "");
    
            map.put("name", name);
            map.put("passwd", passwd);
    
            return map;
        }
    }

    编写测试函数

        private Button button1;
        private Button button2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button1 = (Button) findViewById(R.id.button1);
    
            button1.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    SharedPreference sharedPreference = new SharedPreference(MainActivity.this);
                    boolean flag = sharedPreference.saveMessage("furong", "123456");
    
                    Toast.makeText(MainActivity.this, "---->" + flag, 1).show();
                }
            });
    
            button2 = (Button) findViewById(R.id.button2);
            button2.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Map<String, Object> map;
    
                    SharedPreference sharedPreference = new SharedPreference(MainActivity.this);
                    map = sharedPreference.getMessage();
    
                    Toast.makeText(MainActivity.this, map.toString(), 1).show();
                }
            });
        }

    写测试

    image

    读测试

    image

  • 相关阅读:
    python——二分查找算法
    python实现二分查找
    git merge 与 git rebase的区别
    mysql查询表死锁和结束死锁的方法
    mysql的undo log和redo log
    Python中给List添加元素的4种方法
    输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字
    Python中生成器和迭代器的区别(代码在Python3.5下测试):
    mysql锁
    每天一个linux命令(46):vmstat命令
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11806286.html
Copyright © 2020-2023  润新知