Android保存用户名等小数据使用SharedPreference

来源:互联网 发布:格林第七场数据 编辑:程序博客网 时间:2024/04/29 17:55

写config.xml文件

 SharedPreferences sharedPreferences = getSharedPreferences("config", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("name", userNameText.getText().toString());
    editor.putString("password", passwordText.getText().toString());
    editor.putString("ip", ipText.getText().toString());
    editor.commit();

读config.xml文件

 SharedPreferences sharedPreferences = getSharedPreferences("config",Activity.MODE_PRIVATE);
  if(sharedPreferences != null)
  {
   String nameconfig = sharedPreferences.getString("name", "");
   String passwordconfig = sharedPreferences.getString("password", "");
   String ipconfig = sharedPreferences.getString("ip", "");
   userNameText.setText(nameconfig);
   passwordText.setText(passwordconfig);
   ipText.setText(ipconfig);
  }

0 0
原创粉丝点击