gpt4 book ai didi

java - 带有单选按钮的对话框保存共享首选项,但不保存行为

转载 作者:行者123 更新时间:2023-12-01 12:05:40 25 4
gpt4 key购买 nike

我可以使用一些单选按钮在 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 preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
index = preferences.getInt("choice",-1);
Log.i("Shared", "Y "+index);

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

最佳答案

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

关于java - 带有单选按钮的对话框保存共享首选项,但不保存行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27640219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com