只有當我試圖讓具有消息和一個複選框對話選擇題的項目。我使用下面的代碼AlertDialog表示不使用消息
private void displayWarning() {
SharedPreferences prefs;
final String PREFS_NAME = "UserData";
final String PREF_SHOW_WARNING_KEY = "show_warning";
prefs = this.getActivity().getSharedPreferences(PREFS_NAME, 0);
final String[] items = {"do not show again"};
final boolean[] itemsChecked = {false};
boolean displayWarnings = prefs.getBoolean(PREF_SHOW_WARNING_KEY, true);
if (displayWarnings) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("this is a warning")
.setCancelable(false)
.setMultiChoiceItems(items, itemsChecked, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
itemsChecked[which] = false;
} else {
itemsChecked[which] = true;
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do things
}
});
AlertDialog alert = builder.create();
alert.show();
}
if (itemsChecked[0]) {
displayWarnings = false;
}
else {
displayWarnings = true;
}
prefs.edit().putBoolean(PREF_SHOW_WARNING_KEY, displayWarnings).commit();
}
運行此代碼後,複選框和文本「不再顯示」好好嘗試一下得到顯示。當我刪除郵件(「這是一個警告」)時,我確實收到了複選框。此外,當我更改消息以標題,我得到的複選框,但原始消息太長的標題使用...
希望這是SENCE。底線是我想要一個對話框,其中包含消息和複選框供用戶檢查,以便該對話框不會再次顯示。
雖然很高興看到另一種解決方案,工程,我也很感興趣,什麼不順心在我的例子 – MWB
好吧,我測試,我發佈了另一個答案 –