【问题标题】:Creating ids to implement select listener in dialog创建 id 以在对话框中实现选择侦听器
【发布时间】:2017-12-19 13:53:56
【问题描述】:

我正在使用以下代码创建一个对话框,谁创建了多项选择复选框.. 但我不知道如何创建他们的 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();
}

【问题讨论】:

  • 要生成 ids 你可以使用View.generateViewId(),参考here,同样的问题here

标签: java android dialog


【解决方案1】:

使用您的自定义布局创建一个简单的对话框,而不是警报对话框

   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) {

        }
    }); 

【讨论】:

  • 可以给我dialog_layout.xml的代码吗?
  • 您只需要按照自己的喜好进行布局即可。
猜你喜欢
  • 2021-12-18
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 2021-11-28
相关资源
最近更新 更多