• 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

  • 相关阅读:
    Hdu 1094 A+B for Input-Output Practice (VI)
    Hdu 1091 A+B for Input-Output Practice (III)
    Hdu 1092 A+B for Input-Output Practice (IV)
    Hdu 1087 Super Jumping! Jumping! Jumping!
    scala学习笔记2(类,继承,抽象类)
    scala学习笔记1(表达式)
    把以前写的几个Linux Framebuffer小工具放到github上了,直接去下吧,别找我要了
    一位台湾朋友刚建的一个FTK的论坛,欢迎加入讨论
    Linux高端内存的由来
    read系统调用深度剖析
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11806286.html
Copyright © 2020-2023  润新知