我正在做一个应用程序,根据按下的按钮改变背景颜色,但我遇到了问题。我想要做的是在我的AlertDialog上的RadioGroup中获取所选颜色,我已经尝试了很多方法而且我无法得到它而且我找不到类似的答案。
public void alertDialogColors()
{
final View toDisplayInDialog = getLayoutInflater().inflate(R.layout.radiogroup, null);
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setView(toDisplayInDialog);
builder2.setMessage("Choose Startup Color");
builder2.setPositiveButton("Set Startup Color", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
RadioGroup myRadioGroup = (RadioGroup)toDisplayInDialog.findViewById(R.id.RadioGroup);
int radioGroupId = myRadioGroup.getCheckedRadioButtonId();
RadioButton myCheckedButton = (RadioButton)toDisplayInDialog.findViewById(radioGroupId);
int index = myRadioGroup.indexOfChild(myCheckedButton);
switch(index)
{
case 0:
saveColors(KEY_COLOR, Color.WHITE);
break;
case 1:
saveColors(KEY_COLOR, Color.BLACK);
break;
case 2:
saveColors(KEY_COLOR, Color.RED);
break;
case 3:
saveColors(KEY_COLOR, Color.YELLOW);
break;
case 4:
saveColors(KEY_COLOR, Color.GREEN);
break;
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.create()
.show();
}
答案 0 :(得分:1)
试试这个..
public void saveButtons(String temp, boolean bool)
{
SharedPreferences.Editor ed = myPrefs.edit();
ed.putBoolean(temp, bool);
ed.commit();
}
//Alert Dialog Colors
public void alertDialogColors()
{
final View toDisplayInDialog = getLayoutInflater().inflate(R.layout.radiogroup, null);
final RadioGroup myRadioGroup = (RadioGroup)toDisplayInDialog.findViewById(R.id.RadioGroup);
RadioButton checkRadioButton = (RadioButton) myRadioGroup.getChildAt(indexCheckRadioButton);
checkRadioButton.setChecked(true);
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setView(toDisplayInDialog);
builder2.setMessage("Choose Startup Color");
builder2.setPositiveButton("Set Startup Color", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
int radioGroupId = myRadioGroup.getCheckedRadioButtonId();
RadioButton myCheckedButton = (RadioButton)toDisplayInDialog.findViewById(radioGroupId);
int index = myRadioGroup.indexOfChild(myCheckedButton);
indexCheckRadioButton = index;
switch(index)
{
case 0:
saveColors(KEY_COLOR, Color.WHITE);
saveRadioButton(KEY_RADIOBUTTON, indexCheckRadioButton);
break;
case 1:
saveColors(KEY_COLOR, Color.BLACK);
saveRadioButton(KEY_RADIOBUTTON, indexCheckRadioButton);
break;
case 2:
saveColors(KEY_COLOR, Color.RED);
saveRadioButton(KEY_RADIOBUTTON, indexCheckRadioButton);
break;
case 3:
saveColors(KEY_COLOR, Color.YELLOW);
saveRadioButton(KEY_RADIOBUTTON, indexCheckRadioButton);
break;
case 4:
saveColors(KEY_COLOR, Color.GREEN);
saveRadioButton(KEY_RADIOBUTTON, indexCheckRadioButton);
break;
}
}
})
.setNegativeButton("Cancel", null)
.create()
.show();
}
public void saveRadioButton(String temp3, int color2)
{
SharedPreferences.Editor ed3 = myPrefs.edit();
ed3.putInt(temp3, color2);
ed3.commit();
}