Dialog with radio button save sharedpreferences but not the behaviour

你。 提交于 2020-01-03 05:13:10

问题


I can set the shared preferences in a AlertDialog with some radio buttons:

public void ShowRadioDialog() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = preferences.edit();
        SharedPreferences choiceSettings = getSharedPreferences("currentChoice", 0);
        final int[] currentChoice = {choiceSettings.getInt("currentChoice", 0)};

        final CharSequence[] items={"Rosso","Verde","Blu","Giallo","Arancione"};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Seleziona un colore");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(DialogInterface dialog, int which) {

            if (index == 1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                            getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.redDark));
                            toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
                            Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
                            Log.i("Colors", "Rosso Ok");                  
                        }
                } else if (index ==2) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.green));                 
                    }

                } else if (index == 3){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.primary_blue));      
                    }
                } else if (index == 4){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.yellowDark));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.yellow));      
                    }
                } else if (index == 5){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.dark_orange));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.main_orange));      
                    }
                }
                SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
                Editor editor = preferences.edit();
                editor.putInt("choice", index);
                editor.commit();
            }
        });

        builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                    if ("Rosso".equals(items[which])) {
                        index = 1;
                    } else if ("Verde".equals(items[which])) {
                        index = 2;
                    } else if ("Blu".equals(items[which])) {
                        index = 3;
                    } else if ("Giallo".equals(items[which])) {
                        index = 4;
                    } else if ("Arancione".equals(items[which])) {
                        index = 5;
                    }
            }
        });
        builder.show();
    }

In onCreate of the MainActivity i fetch the sharedpreferences in this way:

SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
        index = preferences.getInt("choice",-1);
        Log.i("Shared", "Y "+index);

The log is correct! index is in the right position as saved. But it not do the condition in the dialog. I want change the color of status bar and toolbar. When you tap in the dialog the radio button works, but when exit from app and then open it again the colors come back to default. But the state of radio button is saved.. It's so strange..


回答1:


Do this in onCreate:

 SharedPreferences preferences = getSharedPreferences("myPref",   getApplicationContext().MODE_PRIVATE);
    index = preferences.getInt("choice",-1);

 if (index == 1) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.redDark));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
                        Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
                        Log.i("Colors", "Rosso Ok");                  
                    }
            } else if (index ==2) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
                    toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.green));                 
                }

            } else if (index == 3){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
                    toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.primary_blue));      
                }
            } else if (index == 4){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.yellowDark));
                    toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.yellow));      
                }
            } else if (index == 5){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.dark_orange));
                    toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.main_orange));      
                }


来源:https://stackoverflow.com/questions/27640219/dialog-with-radio-button-save-sharedpreferences-but-not-the-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!