2015-04-03 4 views
0

Что я хочу сделать, это изменить цвет макета, когда я нажимаю кнопку, но, однако, я хочу, чтобы этот цвет был тем, что когда-либо менял навсегда. В какой-то момент он меняет цвет на то, что я указал, но если я вернусь или снова открою программу, он вернется к нормальной стадии.Как изменить цвет фона макета и сохранить этот цвет навсегда

XML-файл;

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="#fffcfdff" 
android:weightSum="1"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1" 
    android:id="@+id/linearLayout2"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="381dp" 
     android:layout_height="wrap_content" 
     android:background="#5DAD68" 
     android:id="@+id/toppartofthescreen"> 

<Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/backgroundcolour" 
      android:layout_below="@+id/linearLayout2" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="133dp" /> 

    </LinearLayout> 
</LinearLayout> 

Это FIle Java;

public class ChangeBackground extends FragmentActivity { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.example_layout); 

findViewById(R.id.backgroundcolour).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      findViewById(R.id.toppartofthescreen).setBackgroundColor(Color.parseColor("#ffad4843")); 

     } 
    }); 

} 
} 
+1

Вы должны держать свой цвет, например, в общих настройках и установить его, когда вы идете к применению –

+0

Как это сделать? это слишком сложно реализовать? –

+0

Я отправлю ответ –

ответ

2

Вы должны держать свой цвет, например, в SharedPreferences и установить его, когда вы идете к применению. Обновите код, как показано в следующем примере:

public class ChangeBackground extends FragmentActivity { 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.example_layout); 
     final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
     int color = sharedPreferences.getInt("color", -1); 
     if(color != -1){ 
      findViewById(R.id.toppartofthescreen).setBackgroundColor(color); 
     } 
     findViewById(R.id.backgroundcolour).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       findViewById(R.id.toppartofthescreen).setBackgroundColor(Color.parseColor("#ffad4843")); 
       sharedPreferences.edit().putInt("color",Color.parseColor("#ffad4843")).apply(); 
      } 
     }); 

    } 
} 
+0

Я пробовал этот код, который вы предоставили, но после возвращения и возвращения в приложение цвет изменился на что было раньше. Таким образом, он не сохранил его в общих предпочтениях –

+0

Я забыл про применить –

+0

попробуйте сейчас sharedPreferences.edit(). PutInt («color», Color.parseColor («# ffad4843»)) .apply(); –

1

Вы можете попробовать использовать SharedPreferences:

final SharedPreferences preference = getSharedPreferences("my_prefs", Context.MODE_PRIVATE); 
    if(preference.getBoolean("showBgColor", false)) 
    { 
     findViewById(R.id.toppartofthescreen).setBackgroundColor(Color.parseColor("#ffad4843")); 
    } 

    findViewById(R.id.backgroundcolour).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      if(!preference.getBoolean("showBgColor", false)) { 
       SharedPreferences.Editor editor = preference.edit(); 
       editor.putBoolean("showBgColor", true); 
       editor.commit(); 
      } 
      findViewById(R.id.toppartofthescreen).setBackgroundColor(Color.parseColor("#ffad4843")); 

     } 
    }); 
Смежные вопросы