[英]How to simplify sharedpreferences in android?
有沒有一種方法可以簡化以下代碼,從而只需要使用1個設置/編輯器?
SharedPreferences settings = getSharedPreferences("gebruikersnaam", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("gebruikersnaam", givenUsername);
editor.apply();
SharedPreferences settings2 = getSharedPreferences("wachtwoord", 0);
SharedPreferences.Editor editor2 = settings2.edit();
editor2.putString("wachtwoord", givenPassword);
editor2.apply();
SharedPreferences settings3 = getSharedPreferences("url", 0);
SharedPreferences.Editor editor3 = settings3.edit();
editor3.putString("url", givenURL);
editor3.apply();
為什么不將所有字符串都保存在一個sharedPreference中
SharedPreferences settings = getSharedPreferences("myApp", 0);
settings.edit().putString("gebruikersnaam", givenUsername).apply();
settings.edit().putString("wachtwoord", givenPassword).apply();
settings.edit().putString("url", givenURL).apply();
簽出這個很棒的圖書館。
https://github.com/mg6maciej/hrisey
https://github.com/mg6maciej/hrisey/wiki/Preferences
它不僅可以共享首選項,還可以為您節省很多樣板代碼。
它是Lombok的Android特定版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.