我有这个Radio Dialog
case R.id.main_btn_ics:
AlertDialog levelDialog;
// Strings to Show In Dialog with Radio Buttons
final CharSequence[] items = {"Normal","Silent","Vibrate"};
// Creating and Building the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a profile");
builder.setNegativeButton("OK", null);
builder.setCancelable(false);
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch(item)
{
case 0:
audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
break;
case 1:
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
break;
case 2:
audiomanage.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
}
}
});
levelDialog = builder.create();
levelDialog.show();
单击按钮时会出现此广播对话框。问题是它不存储选择。例如,如果我选择Normal,单击“OK”,然后单击Button再次显示Radio Dialog,这没有存储值。为什么这样?我怎么能让他记住这个选择呢?
答案 0 :(得分:0)
builder.setSingleChoiceItems(items,-1,new DialogInterface.OnClickListener(){
用你之前选择的东西替换-1。
final AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int selectedIndex = -1;
if (audiomanage.getRingerMode() == AudioManager.RINGER_MODE_NORMAL){
selectedIndex = 0;
} else if (audiomanage.getRingerMode() == AudioManager.RINGER_MODE_SILENT){
selectedIndex = 1;
} else if (audiomanage.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE){
selectedIndex = 2;
}
builder.setSingleChoiceItems(items, selectedIndex , new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item)
{
case 0:
audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
break;
case 1:
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
break;
case 2:
audiomanage.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
}
}
});