【问题标题】:Dialog with radio button save sharedpreferences but not the behaviour带有单选按钮的对话框保存共享首选项但不保存行为
【发布时间】:2014-12-25 01:18:46
【问题描述】:

我可以使用一些单选按钮在 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 保存在正确的位置。但它不执行对话框中的条件。我想改变状态栏和工具栏的颜色。当您在对话框中点击时,单选按钮有效,但是当退出应用程序然后再次打开它时,颜色会恢复为默认值。但是单选按钮的状态被保存了..太奇怪了..

【问题讨论】:

  • 您是否在 OnCreate 中设置颜色?
  • 在什么意义上?不..我只是获取单选按钮的共享偏好..我到底要做什么?
  • 您必须在进入活动时根据偏好再次设置条形颜色。看我的回答。

标签: java android sharedpreferences android-alertdialog android-radiobutton


【解决方案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));      
                }

【讨论】:

  • 耶耶!有用!嗯,我认为只需要设置首选项.. 谢谢!
  • 不要使用onCreate()中的完整代码。将其移至onResume()
  • 5 分钟,我可以接受 :) 好的,为什么在 onResume() 中?
  • OnResume() 仅适用于简历。如果活动已经在运行。如果您希望在应用程序完成后保留活动。你在 onCreate() 中也需要它
  • 刚刚看到代码正在改变工具栏的颜色,所以应该是onCreate()。但是你对onResume() 的想法是错误的。请查看活动生命周期。
猜你喜欢
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 2012-11-29
  • 2016-06-06
  • 1970-01-01
  • 2019-04-09
  • 2016-12-26
  • 2019-12-18
相关资源
最近更新 更多