1
我目前有一個AlertDialog,用戶可以從三個選項(在名爲itemsOutput1的數組中定義)中進行選擇。雖然我希望能夠根據以前的選擇預測用戶將給出的答案,但通過將默認選擇設置爲先前存儲的共享首選項,此工作正常。在AlertDialog構建器中設置默認首選項
說我知道我想設置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();
這有助於將默認值設置爲sharedPreferences,但我還希望在AlertDialog上突出顯示defaultvalue,以便用戶知道他們是否不選擇任何會自動爲其保存的內容 – Kurt