Android -- SharedPreferences存储信息

背景                                                                                            

SharedPreferences我觉得是最容易上手的,以xml方式存储。

代码                                                                                            

public class share {
    public static void saveInfo(Context context,String username,String password)
    {
        SharedPreferences sp = context.getSharedPreferences("config", context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.commit();
    }
}

主要就是开启edit,然后写数据,然后commit。

SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        String username = sp.getString("username", "");
        String password = sp.getString("password", "");
        usernameText.setText(username);
        passwordText.setText(password);

上面的是读数据。

我是天王盖地虎的分割线                                                                 

源代码:http://pan.baidu.com/s/1dD1Qx01

savefile_share.zip

 

 

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut/p/3720656.html

posted @ 2014-05-17 18:00  我爱物联网  阅读(843)  评论(0编辑  收藏  举报
AmazingCounters.com