java—创建ID以实现对话框中的选择侦听器

egmofgnx  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(219)

我正在用下面的代码创建一个对话框,谁创建了多选复选框。。但我不知道如何创建他们的id来添加点击事件,我是android新手请帮助我..:

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();
}
kmpatx3s

kmpatx3s1#

而不是警报对话框创建一个简单的对话框与您的自定义布局如下

Dialog dialog = new Dialog(MainActivity.this);
    dialog.setContentView(R.layout.dialog_lauout);
    dialog.show();
    Button button = (CheckBox) dialog.findViewById(R.id.button);
    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

相关问题