Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Button oneButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- oneButton=findViewById(R.id.oneButton);
- }
- public void twoButton(View view) {
- oneButton.setBackgroundColor(getResources().getColor(R.color.black));
- }
- private static final String BUTTON_COLOR_PREF = "Button Color";
- private SharedPreferences mPreferences;
- mPreferences = getSharedPreferences(BUTTON_COLOR_PREF, Context.MODE_PRIVATE);
- final Button oneButton = findViewById(R.id.oneButton);
- Button twoButton = findViewById(R.id.twoButton);
- oneButton.setBackgroundColor(mPreferences.getInt(BUTTON_COLOR_PREF, getResources().getColor(R.color.red)));
- //цвет, который будет устанавливаться, пока вы не нажали кнопку первый раз. В данном случае красный. После того как нажмете кнопку он сохранится и будет черный
- twoButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- int colorId = getResources().getColor(R.color.black);
- oneButton.setBackgroundColor(colorId);
- SharedPreferences.Editor editor = mPreferences.edit();
- editor.putInt(BUTTON_COLOR_PREF, colorId);
- editor.apply();
- }
- });
Add Comment
Please, Sign In to add comment