我目前有一个 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();
最佳答案
看看之前的答案 link .
如果我很好地理解了您的问题,这将对您有所帮助。
关于java - 在 AlertDialog 构建器中设置默认首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12798427/