简体   繁体   English

如何保存editText可见性状态(View.GONE)?

[英]how can I save editText visibility state (View.GONE)?

I have a problem with editText visibility. 我对editText可见性有疑问。 In the beginning of my code I've set visibility to GONE 在我的代码的开头,我已将可见性设置为GONE

editText.setVisibility(View.GONE);  

This part is fine, it sets my editText to GONE. 这部分很好,它将我的editText设置为GONE。 However if i minimise my app and then summon it again, editText field becomes Visible. 但是,如果我最小化我的应用程序然后再次召唤它,editText字段将变为可见。 I've tried to save View setting with SharedPreferences, but i don't know how to do it right. 我尝试使用SharedPreferences保存View设置,但我不知道如何正确执行。 I am trying to save View with: 我想用以下方法保存View:

myPrefs = getSharedPreferences("save steps", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putInt("visibility", editText.getVisibility());
editor.commit();   

And then load data back with: 然后加载数据:

editText.setVisibility(myPrefs.getInt("visibility", 0)); 

But this approach crashes my app. 但这种方法崩溃了我的应用程序。 Maybe there are any suggestions how to solve this? 也许有任何建议如何解决这个问题?

The reason is simple. 原因很简单。 When app come back from background, activity restarts (recreate your edittext ). 当应用程序从后台返回时,活动将重新启动(重新创建您的edittext )。

You can use savedInstanceState or put 您可以使用savedInstanceState或put

@Override
public void onResume(){
    super.onResume();
    edittext.setVisibility(View.GONE);
}

EDIT: Change: 编辑:更改:

editText.setVisibility(myPrefs.getInt("visibility", 0)); 

to

editText.setVisibility(myPrefs.getInt("visibility", View.GONE)); 

I can't see the entire code, maybe app crash is caused by null pointer exception.. 我看不到整个代码,也许应用程序崩溃是由空指针异常引起的..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 set.Visibility(View.GONE) 没有使 Button 和 EditText 消失,它的出现就像我什至没有添加代码一样 - set.Visibility(View.GONE) not making the Button and EditText gone, and it's showing up like I didn't even add the code 使用View.GONE时RelativeLayout可见性不起作用 - RelativeLayout Visibility is not working when using View.GONE 我可以进行网页浏览指示器(点)视图吗?默认为“消失”,悬停为“可见” - Can i make pageview indicator(the dot) view.GONE on default and VISIBLE on hover 当我将函数setVisible(View.gone)应用于适配器时,如何从与适配器对应的列表中删除空格? - How to remove the space from a list corresponding to an adapter, when I apply the function setVisible (View.gone) to the adapter? 如何使用 view.GONE 处理 ConstraintLayout 水平和垂直 - How to handle ConstraintLayout horizontal & vertical with view.GONE 从 View.GONE 状态到 View.VISIBLE 的 setVisibility 无法正常工作 - setVisibility from View.GONE state to View.VISIBLE not working properly View.Gone of ParentBottom视图导致问题 - View.Gone of ParentBottom view is causing issue setVisibility(View.GONE) 缩小显示尺寸 - setVisibility(View.GONE) shrinking display size setVisibility(View.GONE)在Fragment中不起作用 - setVisibility(View.GONE) does not work in Fragment Recyclerview中的view.GONE仍然保留空间 - view.GONE in Recyclerview still keeps space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM