0

我可以使用一些单选按钮在 AlertDialog 中设置共享首选项:

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();
    }

在 MainActivity 的 onCreate 中,我以这种方式获取 sharedpreferences:

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

日志是正确的!index保存时处于正确位置。但它不执行对话框中的条件。我想改变状态栏和工具栏的颜色。当您在对话框中点击时,单选按钮有效,但是当退出应用程序然后再次打开它时,颜色会恢复为默认值。但是单选按钮的状态被保存了..太奇怪了..

4

1 回答 1

1

在 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));      
                }
于 2014-12-24T17:18:46.573 回答