在我的应用程序中,我让用户选择“性别”(男,女,其他)。
关于性别,我在应用程序的许多屏幕上向他们显示了头像图标。
有什么办法做到这一点?我不要在所有显示Avatar图标的地方都使用if-else。
我在SharedPreferences中共享性别。
是否有配置文件或设置?我什至无法在Google上搜索有关此特定问题的查询?
帮助
答案 0 :(得分:1)
您可以简单地创建一个类,并可以在任何地方使用它。
AppUtils.java
import {MyComponent} from "./thePath"
AppUtils类的使用:
public class AppUtils {
public static int getAvtarFromGender(Context context) {
if (isMale(context)) {
return R.drawable.male_icon;
} else {
return R.drawable.female_icon;
}
}
private static boolean isMale(Context context) {
SharedPreferences sharedpreferences = context.getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
return sharedpreferences.getBoolean("key_for_gender_preff", false);
}
public void savePreffIsMale(Context context, boolean isMale) {
SharedPreferences sharedpreferences = context.getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(key_for_gender_preff, isUserLoggedIn);
editor.apply();
}
}