How to hide views for always

2019-02-27 14:36发布

Can somebody tell me how to hide views for always?

I'm hiding a view with view.setVisibility(view.GONE); but when reopen the app I have to re-hide the view. I want a view hide for always when button is clicked until clearing app data or uninstalling. Thanks!

4条回答
Juvenile、少年°
2楼-- · 2019-02-27 15:02

Your current approach is the programmatic one and is probably what you will have to use if you want a button to toggle the visibility of a view. The only other option of which I am aware would be to disable visibility from the XML layout file:

<TextView
    android:visibility="gone"
    ... />
查看更多
一夜七次
3楼-- · 2019-02-27 15:06

There is not option to permanent hide but you can achieve by this way

1.Remove the specific view by

parent.removeViewAt(index);

parent.removeView(view);
  1. you have to save the state to show/hide the view on permanent storage like

    Share preference / databases

Shared Preference Example

1.store value in sharedPrefernces:

SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("storevalue", false);
editor.commit();

2.get value from sharedPreferences:

SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
preferences.getBoolean("storevalue", false);

if(preferences.getBoolean("storevalue", false))
{
view.setVisibility(View.VISIBLE);
}
else
view.setVisibility(View.GONE);
查看更多
smile是对你的礼貌
4楼-- · 2019-02-27 15:06

You have to use SharedPreferences, when you hide the View then store a value in SharedPreferences, and when you come back to the app get value from SharedPreferences and according to that value hide your View by the same method that you are using.

SharedPreferences sp = getSharedPreferences("PREFS_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor et = sp.edit();
et.putBoolean("isViewHide", true);
et.commit();

Getting value from SharedPreferences

SharedPreferences sp = getSharedPreferences("PREFS_NAME", Context.MODE_PRIVATE);
boolean cb1 = sp.getBoolean("isViewHide", false);

reference from SharedPreferences example

查看更多
地球回转人心会变
5楼-- · 2019-02-27 15:08

You need to save state by creating flag for hide and unhide view in SharedPreference and basis of that flag you need to GONE or VISIBLE View.

查看更多
登录 后发表回答