如何保存可见性android
Posted
技术标签:
【中文标题】如何保存可见性android【英文标题】:how to save Visibility android 【发布时间】:2020-02-25 01:12:10 【问题描述】:我试图保存我的卡片视图的可见性状态,起初我的视图已经消失,但如果用户按下按钮卡片视图将显示。有人可以帮助我并告诉我如何存储可见性,我在这个主题上找不到任何东西
非常感谢。
我尝试了 shardPrefs 但它不起作用
@Override
protected void onCreate(final Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cv1= (CardView) findViewById(R.id.cv1);
cv2= (CardView) findViewById(R.id.cv2);
cv3= (CardView) findViewById(R.id.cv3);
cv4= (CardView) findViewById(R.id.cv4);
cv5= (CardView) findViewById(R.id.cv5);
cv6= (CardView) findViewById(R.id.cv6);
cv7= (CardView) findViewById(R.id.cv7);
cv8= (CardView) findViewById(R.id.cv8);
cv9= (CardView) findViewById(R.id.cv9);
AddBar = findViewById(R.id.AddBar);
AddVoorraad =findViewById(R.id.AddVoorraad);
AddBar.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
clickcount = clickcount + 1;
if (clickcount == 1)
cv1.setVisibility(View.VISIBLE);
if (clickcount == 2)
cv2.setVisibility(View.VISIBLE);
if (clickcount == 3)
cv3.setVisibility(View.VISIBLE);
if (clickcount == 4)
cv4.setVisibility(View.VISIBLE);
if (clickcount == 5)
cv5.setVisibility(View.VISIBLE);
if (clickcount == 6)
cv6.setVisibility(View.VISIBLE);
if (clickcount==7 )
cv7.setVisibility(View.VISIBLE);
);
AddVoorraad.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
clickcountV=clickcountV+1;
if (clickcountV==1)
cv8.setVisibility(View.VISIBLE);
if (clickcount==2)
cv9.setVisibility(View.VISIBLE);
);
cv1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
);
cv2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
);
cv3.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
);
`
【问题讨论】:
所以你想保存哪些按钮是可见的,以便下次应用运行时恢复它? 【参考方案1】:尝试在条件下将每个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();
);
【讨论】:
非常好!非常感谢!它有效,我做了一些调整,因为我遇到了一个小错误。因此,如果有人遇到同样的错误,请遵循以下命令:***.com/questions/28515049/…【参考方案2】:我不知道您具体尝试了什么,但如果您只想存储状态以备后用,您当然可以使用 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
【讨论】:
首先感谢您的回复!我想创建一个库存应用程序。用户将能够添加最多 7 个的薮条,与“添加 voorraad(表示库存)相同,但最多为 3 个。 欢迎您,祝您的项目好运!【参考方案3】:您可以在配置更改(旋转)时从 onSaveInstanceState 中检索值,但是当您关闭应用程序(后退按钮)时,您需要从持久性存储中检索状态,例如共享首选项。
【讨论】:
以上是关于如何保存可见性android的主要内容,如果未能解决你的问题,请参考以下文章