2017-05-12 28 views
0

我的应用程序中有选项卡式活动。在每个标签页中,我都有一个(切换)按钮,可以通过单击切换到打开和关闭。当我移动到另一个标签并回到相同的位置时,它就会熄灭。我想保持它的价值,以便在切换到其他活动时不会改变。在不同的活动中保持变量值Android Studio

布局(XML)的按钮的代码:

<Button 
    android:id="@+id/b_btnlight" 
    android:layout_width="65dp" 
    android:layout_height="30dp" 
    android:text="ON" 
    android:background="@color/red" 
    android:textColor="@color/black" 
    android:layout_marginLeft="220dp" 
    android:layout_marginTop="500dp"/> 

的相关标签活动:

bbtnlight = (Button) rootView.findViewById(R.id.b_btnlight); 

bbtnlight.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if(bbtnlight.getText()=="ON") 
       {bbtnlight.setText("OFF"); 
        bbtnlight.setBackgroundColor(getResources().getColor(R.color.green));} 
       else 
       {bbtnlight.setText("ON"); 
        bbtnlight.setBackgroundColor(getResources().getColor(R.color.red));} 
      } 
     }); 

当按钮为OFF时,它的颜色是红色和文本等于ON且反之亦然。对于其他选项卡活动,我有同样的事情。我很感激在这种情况下你能否帮助我。谢谢。

回答

1

一种选择是,以与静态变量的一类应用程序生命周期

public class GlobalVars { 
    public static boolean isLightOn; 
} 

这样,您就可以得到和在应用程序的任何地方设置GlobalVars.isLightOn期间举办的值。当您使用开关时将其设置为true,并且当您返回时,检查该变量是否为真。 当你关闭你的应用程序中的变量将被重置为假

另一种选择是使用SharedPreferences来存储数据,这样一来,当你关闭你的应用程序,数据会在那里,如果你再次打开它

+0

谢谢,我需要这个GlobalVars实际。它为我工作。 – srt

2

在Android中,如果您的活动不在屏幕上,您将无法确定活动的内容何时会消失。如果您希望应用程序记住值,则必须找到永久或半永久存储位置的地方,即使用户切换到另一个活动,另一个应用程序,关闭了屏幕,甚至关闭了设备。您不能假定仅仅因为该活动不再在用户完成它的屏幕上。例如,如果用户在使用您的应用程序时接到电话,他们可能希望返回他们离开的地方。

有几个地方可以在Android中存储持久性数据。以下是前2:

  1. 共享偏好 - https://developer.android.com/reference/android/content/SharedPreferences.html
  2. SqlLite - https://developer.android.com/training/basics/data-storage/databases.html
1

使用SharedPreference

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode 
Editor editor = pref.edit(); 

存储数据

editor.putBoolean("key_name", true); // Storing boolean - true/false 
editor.putString("key_name", "string value"); // Storing string 
editor.putInt("key_name", "int value"); // Storing integer 
editor.putFloat("key_name", "float value"); // Storing float 
editor.putLong("key_name", "long value"); // Storing long 

editor.commit(); // commit changes 

检索数据

pref.getString("key_name", null); // getting String 
pref.getInt("key_name", null); // getting Integer 
pref.getFloat("key_name", null); // getting Float 
pref.getLong("key_name", null); // getting Long 
pref.getBoolean("key_name", null); // getting boolean 
1

您只需设置它SharedPreferences

SharedPreferences pref = getSharedPreferences(MODE_PRIVATE); 
Editor editor = pref.edit(); 

onCreate(){ 
    checkButton(); 
    bbtnlight.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       checkButton(); 
      } 
     }); 
} 

private void checkButton() { 
    if (pref.getBoolean("is_button_checked", false)) { 
     bbtnlight.setText("ON"); 
     bbtnlight.setBackgroundColor(getResources().getColor(R.color.red)); 
     editor.putBoolean("is_button_checked", true); 
    } else { 
     bbtnlight.setText("OFF"); 
     bbtnlight.setBackgroundColor(getResources().getColor(R.color.green)); 
     editor.putBoolean("is_button_checked", false); 
    } 
}