Guest User

Untitled

a guest
Aug 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. private Button oneButton;
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7.  
  8. oneButton=findViewById(R.id.oneButton);
  9. }
  10.  
  11. public void twoButton(View view) {
  12. oneButton.setBackgroundColor(getResources().getColor(R.color.black));
  13. }
  14.  
  15. private static final String BUTTON_COLOR_PREF = "Button Color";
  16. private SharedPreferences mPreferences;
  17.  
  18.  
  19.  
  20. mPreferences = getSharedPreferences(BUTTON_COLOR_PREF, Context.MODE_PRIVATE);
  21. final Button oneButton = findViewById(R.id.oneButton);
  22. Button twoButton = findViewById(R.id.twoButton);
  23. oneButton.setBackgroundColor(mPreferences.getInt(BUTTON_COLOR_PREF, getResources().getColor(R.color.red)));
  24. //цвет, который будет устанавливаться, пока вы не нажали кнопку первый раз. В данном случае красный. После того как нажмете кнопку он сохранится и будет черный
  25.  
  26. twoButton.setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View view) {
  29. int colorId = getResources().getColor(R.color.black);
  30. oneButton.setBackgroundColor(colorId);
  31.  
  32. SharedPreferences.Editor editor = mPreferences.edit();
  33. editor.putInt(BUTTON_COLOR_PREF, colorId);
  34. editor.apply();
  35.  
  36. }
  37. });
Add Comment
Please, Sign In to add comment