开发者

Android - Disable all other items on dialog when clicked on another

开发者 https://www.devze.com 2023-04-03 13:59 出处:网络
How to disable all other items on dialog when clicked on another? Below is my code final CharSequence[] items = {\"Red\", \"Green\", \"Blue\"};

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.      
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号