2017-07-14 66 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