3 回答
TA贡献1111条经验 获得超0个赞
尝试根据条件存储每个CardView内部的可见性。SharedPrefs并在 CardView 初始化期间检索并设置它们。检查下面的代码:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int isVisible = sharedPreferences.getInt("cv1", View.GONE);
cv1.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv2", View.GONE);
cv2.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv3", View.GONE);
cv3.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv4", View.GONE);
cv4.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv5", View.GONE);
cv5.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv6", View.GONE);
cv6.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv7", View.GONE);
cv7.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv8", View.GONE);
cv8.setVisibility(isVisible);
isVisible = sharedPreferences.getInt("cv9", View.GONE);
cv9.setVisibility(isVisible);
AddBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickcount = clickcount + 1;
if (clickcount == 1) {
cv1.setVisibility(View.VISIBLE);
sharedPreferences.edit().putInt("cv1", View.VISIBLE).commit();
}
if (clickcount == 2) {
cv2.setVisibility(View.VISIBLE);
sharedPreferences.edit().putInt("cv2", View.VISIBLE).commit();
}
if (clickcount == 3) {
cv3.setVisibility(View.VISIBLE);
sharedPreferences.edit().putInt("cv3", View.VISIBLE).commit();
}
if (clickcount == 4) {
cv4.setVisibility(View.VISIBLE);
sharedPreferences.edit().putInt("cv4", View.VISIBLE).commit();
}
if (clickcount == 5) {
cv5.setVisibility(View.VISIBLE);
sharedPreferences.edit().putInt("cv5", View.VISIBLE).commit();
}
if (clickcount == 6) {
cv6.setVisibility(View.VISIBLE);
sharedPreferences.edit().putInt("cv6", View.VISIBLE).commit();
}
if (clickcount==7 ){
cv7.setVisibility(View.VISIBLE);
sharedPreferences.edit().putInt("cv7", View.VISIBLE).commit();
}
}
});
AddVoorraad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickcountV=clickcountV+1;
if (clickcountV==1) {
cv8.setVisibility(View.VISIBLE);
sharedPreferences.edit().putInt("cv8", View.VISIBLE).commit();
}
if (clickcountV==2) {
cv9.setVisibility(View.VISIBLE);
sharedPreferences.edit().putInt("cv9", View.VISIBLE).commit();
}
}
});
TA贡献1820条经验 获得超2个赞
当配置更改(旋转)时,您可以从 onSaveInstanceState 检索值,但是当您关闭应用程序(后退按钮)时,您需要从持久性存储中检索状态,例如共享首选项。
TA贡献1850条经验 获得超11个赞
我不知道你到底尝试了什么,但如果你想做的只是存储状态以供以后使用,你当然可以使用 SharedPreferences 来实现这一点。您可以执行以下操作将状态保存为表示状态的字符串:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();
editor.putString("cv1_visibility", "VISIBLE");
editor.putString("cv2_visibility", "INVISIBLE");
editor.putString("cv3_visivility", "GONE");
...
editor.commit();
然后你可以像这样读取这些值:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
String cv1_state = prefs.getString("cv1_visibility", "NOT_FOUND");
String cv2_state = prefs.getString("cv2_visibility", "NOT_FOUND");
String cv3_state = prefs.getString("cv3_visibility", "NOT_FOUND");
...
然后根据活动开始时的状态进行适当的处理。请注意,getString()有一个defValue参数(第二个参数),该参数是首选项不存在时要返回的值。
编辑:您可以选择将它们存储为 int,但请注意其中一些常量不是 int。看看这个就明白我的意思了:https ://developer.android.com/reference/android/view/View
添加回答
举报