2017-07-14 40 views
0

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

您的自定义布局,以便生成ID,您就可以使用'View.generateViewId()',参考[这里]( https://developer.android.com/reference/android/view/View.html#generateViewId()),同样的问题[here](https:// stackoverflow .com/a/21000252/2910520) – MatPag

回答

0

相反的警告对话框中创建一个简单的对话框,这样

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

     } 
    }); 
+0

你也可以给我dialog_layout.xml的代码吗? – Niezwm

+0

您需要根据自己的喜好进行布局。 – Anmol