Android:如何永久设置可见性

时间:2016-03-30 22:02:37

标签: java android xml android-studio mobile

这不是'如何设置可见性'的问题。

我有一个活动1和一个活动2,当我开始活动2时,我在一个监听器中设置了从Gone到Visible的布局并且它有效,我的问题是,当我返回到活动1并返回到活动2时,布局的可见性又恢复了。当我离开时,如何使布局保持可见?

Activity2.java:

@Override
 public void onClick(View v) {

        if (FirstLayout.getVisibility() == View.INVISIBLE) {
            FirstLayout.setVisibility(View.VISIBLE);
         }
}

Activity2.xml:

 <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:visibility="invisible"
      android:id="@+id/oneLayout"
      android:animateLayoutChanges="true"
      android:orientation="vertical">

      <!-- one Button -->
      <!-- one TextView -->   

  </LinearLayout>

2 个答案:

答案 0 :(得分:1)

您可以通过以下方法

保存您的上一个州
this.browser.select('select', '223.255.252.246');

并恢复

boolean mState = false;
final String STATE_VISIBILITY = "state-visibility";

// somewhere in the code assign a value to mState 
// i.e. mState = false (if GONE default) and mState = true (if VISIBLE)

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putBoolean(STATE_VISIBILITY, mState);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

更多关于http://developer.android.com/training/basics/activity-lifecycle/recreating.html

答案 1 :(得分:1)

正如上面的评论所述,在你的Activity2.java中:

SharedPreferences sharedPreferences;

并在Activity2的onCreate方法中:

 sharedPreferences=getPreferences(Context.MODE_PRIVATE);
 FirstLayout.setVisibility(sharedPreferences.getBoolean("visibility",false)); 

然后:

 @Override
    public void onClick(View v) {
    if (FirstLayout.getVisibility() == View.INVISIBLE) {
        FirstLayout.setVisibility(View.VISIBLE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putBoolean("visibility",true).commit();
     }
}