对话框,单选按钮来保存共享preferences而不是行为 [英] Dialog with radio button save sharedpreferences but not the behaviour
问题描述
我可以在一个AlertDialog一些单选按钮设置共享preferences:
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();
}
在MainActivity我以这种方式获取的共享preferences的的onCreate:
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..
推荐答案
在的onCreate做到这一点:
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));
}
这篇关于对话框,单选按钮来保存共享preferences而不是行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!