Iam用下面的代碼創建一個對話框,誰創建多選複選框..但我不知道如何創建自己的ID來添加單擊事件,我是新來的android請幫助我..:在對話框中創建ID來實現選擇監聽器
private void showDailog() {
final String[] items = {" Blue", " Red", " Black", " White", " Pink"};
final ArrayList itemsSelected = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select any theme you want : ");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedItemId,
boolean isSelected) {
if (isSelected) {
itemsSelected.add(selectedItemId);
} else if (itemsSelected.contains(selectedItemId)) {
itemsSelected.remove(Integer.valueOf(selectedItemId));
}
}
})
.setPositiveButton("Done!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Your logic when OK button is clicked
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id)
}
});
dialog = builder.create();
dialog.show();
}
您的自定義佈局,以便生成ID,您就可以使用'View.generateViewId()',參考[這裏]( https://developer.android.com/reference/android/view/View.html#generateViewId()),同樣的問題[here](https:// stackoverflow .com/a/21000252/2910520) – MatPag