我正在尝试创建一个允许用户选择背景颜色的选项。但是,当用户选择一个选项时,它默认为绿色。如果我卸载该应用程序,则背景颜色默认为绿色。如果我删除选项绿色,它默认为上面的颜色(蓝色)。我不知道我哪里出错了。任何建议将不胜感激。
public class UserMenu extends Activity implements OnClickListener {
Button preview;
Spinner spinnerColor;
SharedPreferences preferences;
public static String theme = "Blue";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = getSharedPreferences("StylePref", 0);
theme = preferences.getString("THEME", "Red");
if(theme.equals("Red"));
{
setTheme(R.style.Theme_Red);
}
if(theme.equals("Blue"));
{
setTheme(R.style.Theme_Blue);
}
if(theme.equals("Green"));
{
setTheme(R.style.Theme_Green);
}
setContentView(R.layout.activity_user_menu);
System.out.println(theme);
spinnerColor = (Spinner) findViewById(R.id.spinnerColorMenu);
preview = (Button)findViewById(R.id.previewButton);
preview.setOnClickListener(this);
}
public void onClick(View v)
{
SharedPreferences.Editor preferencesEditor = preferences.edit();
String bgColor = spinnerColor.getSelectedItem().toString();
preferencesEditor.putString("THEME", bgColor);
preferencesEditor.commit();
if(bgColor.equals("Red"))
{
//Intent intent = getIntent();
//intent.putExtra("Theme", "Red");
finish();
//startActivity(intent);
}
else if(bgColor.equals("Blue"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Blue");
finish();
startActivity(intent);
}
else if(bgColor.equals("Green"))
{
Intent intent = getIntent();
//intent.putExtra("Theme", "Green");
finish();
startActivity(intent);
}
}
}