我正在写一个简单的程序,涉及某些元素的颜色变化。 对于换色,我必须在SharedMemory中存储颜色值(我从颜色选择器中获得)。我有以下颜色选择器代码:
public class MainActivity extends Activity implements OnColorChangedListener {
private ColorPicker picker;
private SVBar svBar;
private OpacityBar opacityBar;
private Button button;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
picker = (ColorPicker) findViewById(R.id.picker);
svBar = (SVBar) findViewById(R.id.svbar);
opacityBar = (OpacityBar) findViewById(R.id.opacitybar);
button = (Button) findViewById(R.id.button1);
text = (TextView) findViewById(R.id.textView1);
picker.addSVBar(svBar);
picker.addOpacityBar(opacityBar);
picker.setOnColorChangedListener(this);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Color",""+ picker.getColor());
text.setTextColor(picker.getColor());
picker.setOldCenterColor(picker.getColor());
}
});
}
@Override
public void onColorChanged(int color) {
//gives the color when it's changed.
}
}
如何在SharedMemory中存储 picker.getColor()的值?
答案 0 :(得分:1)
@Override
public void onColorChanged(int color) {
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.mycolor), color);
editor.commit();
}
要从共享首选项中获取存储的颜色,请使用此
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int storedColor = sharedPref.getInt(getString(R.string.mycolor), 0);
答案 1 :(得分:1)
用户存储您的颜色值的SharedPreference。
共享首选项是指您可以存储数据的地方,直到设备上存在应用程序时,以及用户是否未通过设置清除应用程序内存。
如果您想存储颜色数组,请参阅此帖子: https://stackoverflow.com/a/12350878/2035885
如果只需要存储一个值,请参阅以下内容:
public static int getColor(Context context)
{
SharedPreferences sharedPreference = context.getSharedPreferences("your_shared_preference_name",Context.MODE_PRIVATE);
return sharedPreference.getInt("key_to_use_to_set_and_retrieve_value", 0);
}
public static void setColor(Context context,int color)
{
SharedPreferences store = context.getSharedPreferences(MenuActivity.class.getSimpleName(),Context.MODE_PRIVATE);
SharedPreferences.Editor edit = store.edit();
edit.putInt("key_to_use_to_set_and_retrieve_value", color);
edit.commit();
}
答案 2 :(得分:0)
将其存储为int
。那就是它的本质。
@Override
public void onColorChanged(int color) {
// omitted: get a SharedPreferences object first
sharedPreferences.edit().putInt("color", color).apply();
}
答案 3 :(得分:0)
// try this way,hope this will help you...
//Set To SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("yourSharePreferenceName", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("pickerColor",picker.getColor() );
editor.commit();
//Get From SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("yourSharePreferenceName", MODE_PRIVATE);
picker.setOldCenterColor(sharedPreferences.getInt("pickerColor",0));
答案 4 :(得分:0)
java是跟随
package com.example.sharedpreferenceexample;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
public class Utils {
public static void savePreferences(Activity activity, String key1,
int value1) {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(activity.getApplicationContext());
Editor editor = sp.edit();
editor.putInt(key1, value1);
editor.commit();
}
public static int readPreferences(Activity activity, String key,
int defaultValue) {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(activity.getApplicationContext());
return sp.getInt(key, defaultValue);
}
}
并按如下方式使用
保存
Utils.savePreferences(MainActivity.this, "colorKey", colorValue);
阅读
Utils.readPreferences(MainActivity.this, "colorKey", 0)