SharedPreferences sp = this.getSharedPreferences("xxx.xml", MODE_PRIVATE);
不需要文件扩展名
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t2();
}
protected void t1() {
SharedPreferences sp = this.getSharedPreferences("xxx.xml", MODE_PRIVATE);
SharedPreferences.Editor spe = sp.edit();
spe.putBoolean("a", true);
spe.putFloat("f", 2222.999f);
spe.putInt("int", new Integer(22));
spe.putString("String", "testXML");
Set<String> xx = new HashSet<String>();
xx.add("a");
xx.add("b");
xx.add("c");
xx.add("d");
spe.putStringSet("set", xx);
spe.commit();
}
protected void t2() {
SharedPreferences sp = getSharedPreferences("xxx.xml", MODE_PRIVATE);
TextView tv = (TextView) findViewById(R.id.t1);
HashSet<String> sset = (HashSet<String>) sp.getStringSet("set", null);
Iterator<String> index = sset.iterator();
while (index.hasNext()) {
String temp = index.next();
if (temp.equals("a")) {
System.out.println(temp);
}
}
System.out.println(sp.getBoolean("a",true));
System.out.println(sp.getFloat("f",0));
System.out.println(sp.getInt("int",0));
System.out.println(sp.getInt("int",0));
}
}
0 件のコメント:
コメントを投稿