本文介绍了选择一种颜色从holocolorpicker然后使用共享preferences保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想救他的颜色从holocolorpicker采摘和其他活动使用它

am trying to save he color picked from the holocolorpicker and use it in another activity

之前的设置活动onCreate方法,我把这个线

before the onCreate method in the settings activity i put this lines

private String SettingsTAG0 = "backcolorValue";
private SharedPreferences backcolorprefs;
private static int backcolorValue = 0;

后在设置活动onCreate方法,我把这个线

after the onCreate method in the settings activity i put this lines

public void onColorChanged(int color) {
    ColorPicker picker0 = (ColorPicker) findViewById(R.id.backpicker);
    backcolorValue = picker0.getColor();
    Editor editor0 = backcolorprefs.edit();
    editor0.clear();
    editor0.putInt("back_colorcode", backcolorValue);
    editor0.commit();
}

在其他活动的onCreate方法,我把这个线

before the onCreate method of the other activity i put this lines

private String SettingsTAG0 = "backcolorValue";
private SharedPreferences backcolorprefs;
private static int backcolorValue = 0;

在其他活动的onCreate方法,我把这个线

in the onCreate method of the other activity i put this lines

backcolorprefs = getSharedPreferences(SettingsTAG0, 0);
    backcolorprefs.getInt("back_colorcode", backcolorValue);
    View view = this.getWindow().getDecorView();
    view.setBackgroundColor(backcolorValue);

我是超级新手,Android和Java,但我做一个尝试,但没有什么是发生

i am a super newbie to android and java but i make a try but nothing is happened

任何帮助,请

推荐答案

这是共享preference使用方式:

This is how sharedpreference used:

// put int
SharedPreferences sharedpreferences = getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putInt("back_colorcode", backcolorValue);
editor.commit();

// get int
SharedPreferences sharedpreferences = getSharedPreferences("MyPreference", Context.MODE_PRIVATE);
int backcolorValue = sharedpreferences.getInt("back_colorcode", 0)

这篇关于选择一种颜色从holocolorpicker然后使用共享preferences保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 10:44