多选对话框

实现的方法为:

public void click3(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle(“请选择您喜欢的水果:”);
final String []items={“苹果”,”梨”,”香蕉”,”鸡蛋”,”火龙果”};
final boolean[]checked={false,false,false,false,false};
builder.setMultiChoiceItems(items, checked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {

            Toast.makeText(getApplicationContext(),items[which]+isChecked,Toast.LENGTH_SHORT).show();
            checked[which]=isChecked;
        }
    });

    builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            StringBuffer sb=new StringBuffer();
            for(int i=0;i<checked.length;i++){
                if (checked[i]){
                    sb.append(items[i]+" ");
                }
            }
            Toast.makeText(getApplicationContext(),"您喜欢的水果为:"+sb.toString(),Toast.LENGTH_SHORT).show();
        }
    });
    builder.show();
}

你可能感兴趣的:(对话框)