加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Android 共享参数 SharedPreferences

发布时间:2020-12-15 07:16:41 所属栏目:Java 来源:网络整理
导读:完成共享参数的读写 public class SharedPreference { private Context context; public SharedPreference(Context context) { // TODO Auto-generated constructor stub this .context = context; } boolean saveMessage(String name,String passwd) { bool

完成共享参数的读写

public class SharedPreference {

    private Context context;

    public SharedPreference(Context context) {
        // TODO Auto-generated constructor stub

        this.context = context;
    }

    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" 将数据持久化到存储介质中
        flag = editor.commit();
        return flag;
    }

    public Map<String,Object> getMessage() {
        Map<String,Object> map = new HashMap<String,1)">();
        SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo");
        String passwd = sharedPreferences.getString("passwd",1)">);

        map.put("name" map;
    }
}

编写测试函数

private Button button1; 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 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( TODO Auto-generated method stub Map<String,1)"> map; SharedPreference sharedPreference = ); map = sharedPreference.getMessage(); Toast.makeText(MainActivity.).show(); } }); }

写测试

读测试

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读