How to disable all other items on dialog when clicked on another? Below is my code
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Select Sources");
builder.setMultiChoiceItems(items, null, new OnMultiChoiceClickListener() {
@Override
开发者_如何学编程 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(items[which] == "Red"){
//Disabled Green and Blue items
}
}
});
AlertDialog alert = builder.create();
alert.show();
You may do it so, that you don't use built-in Dialog's check-item list, but provide your own, where you can do anything on items.
You may use CursorAdapter (extend this class) and set it by setAdapter on a ListView in your layout inside dialog (use DialogBuilder.setView to set the list view).
In CursorAdapter implement mainly newView (where you create compound view made of item name and a checkbox encapsulated in LinearLayout, you may inflate this from resources. And implement bindView to setup single item - it's name, checkbox state and enabled/disabled state.
In short: AlertDialog offers only simple multi-option list. If you need customizations, you must go longer way by using own list adapter.
Modify the following code according to your needs:
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(items[which] == "Red"){
((AlertDialog) dialog).getListView().setItemChecked(which,false);//this line will help you disabling the other options.
}
}
精彩评论