大家好我有一个问题。 我有一个按钮,有两个选项,如女孩检查和未选中,我对男孩也一样。在我的片段中,我需要选择一个并将其保存在SharedPreferences中。
我有配置按钮:
ORDER BY
我也有一种方法可以保存 - 但我认为我做了一些不好的事情,因为这不起作用
private void setSexButtons() {
sexButtonBoy.setOnClickListener(view -> {
sexButtonBoy.setSelected(true);
sexButtonBoy.setScaleX(1.4f);
sexButtonBoy.setScaleX(1.4f);
sexButtonGirl.setSelected(false);
sexButtonGirl.setScaleY(1.0f);
sexButtonGirl.setScaleX(1.0f);
Settings.setSelectedIem(true);
});
sexButtonGirl.setOnClickListener(view -> {
sexButtonBoy.setSelected(false);
sexButtonGirl.setSelected(true);
sexButtonGirl.setScaleX(1.4f);
sexButtonGirl.setScaleX(1.4f);
sexButtonBoy.setScaleY(1.0f);
sexButtonBoy.setScaleX(1.0f);
Settings.setSelectedIem(true);
});
}
请告诉我如何做好这件事。
对不起,我想念它在这里:
public static void setSelectedIem(boolean selectedIem) {
getPreferences().edit()
.putBoolean(SELECTED_SEX, selectedIem)
.apply();
}
private static final String SELECTED_SEX = "selectedSex";
和onCreate
privated SharedPreferences sharedPreferences;
这是主要方法:
sharedPreferences = getSharedPreferences("me.fast.app", MODE_PRIVATE);
更新了新方法
private static SharedPreferences getPreferences() {
return ApplicationFast.sharedPreferences;
}
答案 0 :(得分:0)
我认为你的问题是保存一个布尔值,因为对于两性而言,你都在保存“真实”。
你应该这样做:
private void setSexButtons() {
//If you want to recover the settings do this:
sexButtonBoy.setSelected(Settings.isSelected());
sexButtonGirl.setSelected(!Settings.isSelected());
sexButtonBoy.setOnClickListener(view -> {
sexButtonBoy.setSelected(true);
sexButtonBoy.setScaleX(1.4f);
sexButtonBoy.setScaleX(1.4f);
sexButtonGirl.setSelected(false);
sexButtonGirl.setScaleY(1.0f);
sexButtonGirl.setScaleX(1.0f);
Settings.setSelectedIem(true);
});
sexButtonGirl.setOnClickListener(view -> {
sexButtonBoy.setSelected(false);
sexButtonGirl.setSelected(true);
sexButtonGirl.setScaleX(1.4f);
sexButtonGirl.setScaleX(1.4f);
sexButtonBoy.setScaleY(1.0f);
sexButtonBoy.setScaleX(1.0f);
Settings.setSelectedIem(false);
});
}
然后:
public static void setSelectedIem(boolean selectedIem) {
getPreferences().edit()
.putBoolean(IS_BOY, selectedIem)
.apply();
}
private static final String IS_BOY = "isboy";