我是Android新手。我正在開發一個應用程序,我需要顯示一個對話框,用戶可以在其中選擇多個項目。爲此,我想爲對話框中的每個項目添加一個複選框。AlertDialog複選框在Android中
我StackOverflow上閱讀以下文章:
How to add a check box to an alert dialog和
Android Checkbox Dialog (Easy)
其中一個職位的描述瞭如何創建XML定義除該複選框以膨脹的對話框中,但我不能讓它工作。
我是Android新手。我正在開發一個應用程序,我需要顯示一個對話框,用戶可以在其中選擇多個項目。爲此,我想爲對話框中的每個項目添加一個複選框。AlertDialog複選框在Android中
我StackOverflow上閱讀以下文章:
How to add a check box to an alert dialog和
Android Checkbox Dialog (Easy)
其中一個職位的描述瞭如何創建XML定義除該複選框以膨脹的對話框中,但我不能讓它工作。
在AlertDialog添加複選框在博客是很好的解釋與例如AlertDialog With CheckBox In Android
您可以訪問博客AlertDialog With CheckBox In Android更多的細節,下面是來自同一個博客採取
final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
// arraylist to keep the selected items
final ArrayList seletedItems=new ArrayList();
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Select The Difficulty Level")
.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
seletedItems.add(indexSelected);
} else if (seletedItems.contains(indexSelected)) {
// Else, if the item is already in the array, remove it
seletedItems.remove(Integer.valueOf(indexSelected));
}
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on OK
// You can write the code to save the selected item here
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on Cancel
}
}).create();
dialog.show();
它只適用於如果你沒有setMessage – gregoiregentil
答案可能會更好!在alerBuilder.setMultiChoiceItems()
第二個參數是用來顯示對話框之前,以確定檢查的項目,並確定檢查的項目被點擊肯定按鈕後,那麼該代碼可能是這樣的布爾數組:
AlertDialog.Builder alerBuilder = new AlertDialog.Builder(this);
final DBHelper dbHelper = new DBHelper(this);
final List<MeasurementDevice> totalDeviceList = dbHelper.getAllDevices();
String[] deviceNameArr = new String[totalDeviceList.size()];
final boolean[] selectedItems = new boolean[totalDeviceList.size()];
for(int i = 0 ; i < deviceNameArr.length ; i++){
deviceNameArr[i] = totalDeviceList.get(i).getName();
selectedItems[i] = false;
for(int j = 0 ; j < measurementDeviceArrayList.size() ; j++){
if(measurementDeviceArrayList.get(j).getId() == totalDeviceList.get(i).getId()){
selectedItems[i] = true;
break;
}
}
}
alerBuilder.setMultiChoiceItems(deviceNameArr,selectedItems,new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
Log.e("CheckStatus",String.valueOf(b));
}
}).setPositiveButton(R.string.ok_ww,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int ii) {
for(int i = 0 ; i < selectedItems.length ; i++)
Log.e("Sizzz",String.valueOf(selectedItems[i]));
}
}).setCancelable(false).setTitle(R.string.add_device).create().show();
如果你想添加CheckBox
與圖標到AlertDialog
你可以定製它像
public class DialogMultipleChoiceAdapter extends BaseAdapter {
LayoutInflater mLayoutInflater;
List<Item> mItemList;
public DialogMultipleChoiceAdapter(Context context, List<Item> itemList) {
mLayoutInflater = LayoutInflater.from(context);
mItemList = itemList;
}
@Override
public int getCount() {
return mItemList.size();
}
@Override
public Item getItem(int i) {
return mItemList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
public List<Item> getCheckedItem() {
List<Item> checkedItemList = new ArrayList<>();
for (Item item : mItemList) {
if (item.isChecked()) {
checkedItemList.add(item);
}
}
return checkedItemList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Item item = getItem(position);
holder.tvTitle.setText(item.getTitle());
holder.ivImage.setImageResource(item.getImageRes());
holder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
item.setChecked(!item.isChecked());
updateItemState(holder, item.isChecked());
}
});
updateItemState(holder, item.isChecked());
return convertView;
}
private void updateItemState(ViewHolder holder, boolean checked) {
holder.root.setAlpha(checked ? 1 : 0.8f);
holder.checkbox.setChecked(checked);
}
private static class ViewHolder {
View root;
TextView tvTitle;
ImageView ivImage;
CheckBox checkbox;
ViewHolder(View view) {
root = view;
tvTitle = view.findViewById(R.id.text);
ivImage = view.findViewById(R.id.image);
checkbox = view.findViewById(R.id.checkbox);
}
}
}
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
>
<ImageView
android:id="@+id/image"
android:layout_width="38dp"
android:layout_height="38dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="8dp"
android:layout_toEndOf="@id/image"
tools:text="AA"
/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Item
模型
class Item {
private String title;
private int imageRes;
private boolean checked;
public Item(String title, int imageRes) {
this.title = title;
this.imageRes = imageRes;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getImageRes() {
return imageRes;
}
public void setImageRes(int imageRes) {
this.imageRes = imageRes;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
最後,
List<Item> itemList = new ArrayList<>();
public void show() {
if (itemList.isEmpty()) {
itemList.add(new Item("A", R.mipmap.ic_launcher));
itemList.add(new Item("B", R.mipmap.ic_launcher));
itemList.add(new Item("C", R.mipmap.ic_launcher));
}
final DialogMultipleChoiceAdapter adapter =
new DialogMultipleChoiceAdapter(mContext, itemList);
new AlertDialog.Builder(mContext).setTitle("Select Image")
.setAdapter(adapter, null)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(mContext,
"getCheckedItem = " + adapter.getCheckedItem().size(),
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.show();
}
你必須建立在你必須綁定一個XML罰款,其中有複選框 – CoronaPintu
什麼自定義對話框你面臨的例外? –
發佈一些代碼和錯誤(如果有的話)。 @Pritu Corna也是對的。使用自定義對話框。 –