如何修改builder.setMultiChoiceItems选项布局
时间: 2024-02-22 19:58:28 浏览: 113
您可以使用`setListAdapter()`方法来设置`AlertDialog`的`ListView`布局,然后使用`setOnItemClickListener()`方法来处理被选中的选项。这样,您就可以自定义`ListView`的布局,并添加自己的逻辑来处理选项的选择。以下是一个示例代码:
```
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose items");
final String[] items = {"Item 1","Item 2","Item 3"};
boolean[] checkedItems = {false, false, false};
// Set a custom layout for the list view
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.custom_listview_layout, null);
ListView listView = view.findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter<>(context, R.layout.custom_list_item, items));
// Set the list view in the dialog
builder.setView(view);
// Handle item click events
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Handle item click event
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something when OK button is clicked
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something when Cancel button is clicked
}
});
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
```
在上面的示例代码中,我们使用了自定义的`ListView`布局(`custom_listview_layout.xml`)和自定义的选项布局(`custom_list_item.xml`)。您可以根据自己的需求进行修改。
阅读全文