我在Android中使用ColorPicker
。当我从ColorPicker
设置所需的颜色时,它可以正常工作。但是当我退出并再次重新启动activity
时,我的颜色恢复到默认状态。我已经在两周内解决了这个问题,并尝试了SharedPreferences
的每一件事!这是我的代码:
MainActivity.java:
package com.example.project;
import yuku.ambilwarna.AmbilWarnaDialog;
import yuku.ambilwarna.AmbilWarnaDialog.OnAmbilWarnaListener;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
public class Preferences extends Activity {
String[] pref_list = { "Background Color", "Background Wallpaper" };
RelativeLayout background;
public static final String MY_PREFS_NAME = "MyPrefs";
Editor editor;
SharedPreferences sharedpreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
background = (RelativeLayout) findViewById(R.id.p_rl);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.lv_activity, pref_list);
ListView listView = (ListView) findViewById(R.id.pref_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
colorpicker();
} else {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"), 1);
}
}
});
}
public void colorpicker() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, 0xff0000ff,
new OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
SharedPreferences sharedpreference = getApplicationContext()
.getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
Editor editor = sharedpreference.edit();
editor.putInt("Color code", color);
editor.commit();
Preferences.this.findViewById(R.id.p_rl)
.setBackgroundColor(color);
Preferences.this.findViewById(R.id.pref_list)
.setBackgroundColor(color);
}
});
dialog.show();
}
public void run(View view) {
SharedPreferences sharedpreference = getSharedPreferences(
MY_PREFS_NAME, MODE_PRIVATE);
sharedpreference.getInt("Color code", 0);
editor.commit();
}
}
但这不仅仅是工作!我的代码有什么问题?
答案 0 :(得分:0)
像这样更改你的onItemClick监听器
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
int selectedColor = sharedpreference.getInt("color", 0);
listView.setItemChecked(selectedColor, true);
并将其添加到onCreate
Person
答案 1 :(得分:0)
您需要在onCreate或onResume上获取您在sharedPreference上保存的数据。 因此,一旦应用程序启动,您可以应用您保存的任何颜色。
答案 2 :(得分:0)
//add param in that function
public void colorpicker(int color) {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, color,
new OnAmbilWarnaListener() {
//your code
}
}
//call this function in that way:
colorpicker(getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).getInt("Color code", 0xff0000ff));
所以只需使用保存的数据。目前你不会这样做。