使用Android共享首选项来保存活动背景颜色

时间:2015-03-12 14:54:36

标签: java android sharedpreferences

在我的应用中,我可以使用对话框更改活动背景颜色。

我正在尝试使用共享首选项,以便在我在手机上重新启动应用程序时背景颜色保持不变。

如何更改代码以使其有效?

这是我的代码:

public class HomeActivity extends Activity {



    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);


    int storedPreference = preferences.getInt("storedInt", 0);
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home);               

        if (preferences.getString("clicked", "yes").equals("nope")){
            // it never been clicked
        }else{
            // it has been clicked before do whatever you want with the background
        }

    }

    private static void setButtonPref(Context ctx, String clicked)
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("clicked", clicked);
        editor.commit();
    }


    public void showThemes (){
        ListView listView = new ListView(this);
        listView.setAdapter(new ArrayAdapter<String>(this, R.layout.theme_list, new String[]{"Green", "Pink", "Orange", "Blue"}));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            // arg0 is your adapter, arg1 is the view clicked, arg2 is the position and arg3 is the id.
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                switch(arg2){
                    case(0):
                        View someView = findViewById(R.id.mainLayout);
                        View root = someView.getRootView();
                        root.setBackgroundColor(0xFF00FF00);
                        setButtonPref(context, "yes");

                        /*Window window = this.getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        window.setStatusBarColor(0xFF00FF00);*/


                        break;
                    case(1):
                        Toast.makeText(getApplicationContext(), "Coming soon!", Toast.LENGTH_SHORT).show();

                        break;

                    case(2):
                        Toast.makeText(getApplicationContext(), "Coming soon!", Toast.LENGTH_SHORT).show();
                        break;

                    case(3):
                        Toast.makeText(getApplicationContext(), "Coming soon!", Toast.LENGTH_SHORT).show();
                        break;

                    default:
                        break;
                }
            }
        });
        Dialog dialog = new Dialog(this);
        dialog.setTitle("Themes");
        dialog.setContentView(listView);
        dialog.show();

    }

2 个答案:

答案 0 :(得分:1)

<{1>} onCreate() SharedPreferences preferences null ,因为你必须在onCreate()内初始化它所以放线,

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
在访问它之前在onCreate()内部

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);    
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
      int storedPreference = preferences.getInt("storedInt", 0);
      if (preferences.getString("clicked", "yes").equals("nope")){
          // it never been clicked
      } else {
          // it has been clicked before do whatever you want with the background
      }
      setContentView(R.layout.activity_home);    
}

还有一件事,因为有任何代码会影响您的布局,然后在setContentView(R.layout.activity_home);

之前进行更改

答案 1 :(得分:-1)

更改

editor.commit();

editor.apply();