在AlertDialog构建器中设置默认首选项

时间:2012-10-09 10:48:48

标签: java android default alertdialog

我目前有一个AlertDialog,用户可以从三个选项中选择(在名为itemsOutput1的数组中定义)。虽然我希望能够通过将默认选择设置为先前存储的共享首选项来预测用户将根据之前的选择给出的答案,但这样可以正常工作。

假设我知道我想在itemsOutput1中将默认选项设置为第二项,如何将其应用于.setSingleChoiceItems,以便在出现对话框时已选择第二项?

case 1:
return new AlertDialog.Builder(this)
.setIcon(bmd)
.setTitle("What do you want the output to be?")
.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) 
    {
callShapeActivity();
}
})
.setSingleChoiceItems(itemsOutput1, 0, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//---get the SharedPreferences object---
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

//---set the user inputs to prefo's---
editor.putString(OUTPUT_KEY, itemsOutput1[which]);
editor.commit(); 
}
    }
)
.create();

1 个答案:

答案 0 :(得分:0)

请看上一个答案link

如果我理解你的问题,这将对你有所帮助。