select android 警报对话框中的多复选框下拉复选框

multi select checkable dropdown check boxes in alert dailog in android

我正在尝试在像微调器这样的警报对话框中提醒多选下拉复选框,但我的 android 应用程序中有复选框,有人可以帮忙吗?

它可能无法正常处理触摸事件。我想您将不得不克隆它并开发类似 MultiSelectSpinner 的东西。您可能想咨询 this 答案以获取更多详细信息。

使用 api、dialog.setconteView() 方法在 AlertDialog class 中使用自定义布局。您可以在自定义布局中添加任何您想要的小部件

使用下面的代码:-

AlertDialog.Builder builder = new AlertDialog.Builder(
                context);
        builder.setTitle("Title");
        builder.setMultiChoiceItems(list, null,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                            int indexSelected, boolean isChecked)
                    {


                    }
                })
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() 
                {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) 
                            {

                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) 
                            {
                            }
                        });
        dialog = builder.create();
        dialog.show();

按照您的看法,无法获取AlertDialog中的点击事件。

您问题的最佳解决方案。只需包含自定义布局并在 activity.

中获取 Alertdialog Click 事件
  1. 创建您要设计的 XML 布局
  2. 通过 SetContentView()
  3. 在 activity 中包含 XML 布局
  4. 通过你的activity获取点击事件。

请查看此link以供参考http://www.mkyong.com/android/android-custom-dialog-example/

快乐编码:)

for Custom checkbox dropdown Dialogbox first we need to create MultiCheckAdaptar

public class MultiCheckAdaptar extends RecyclerView.Adapter<MultiCheckAdaptar.MyViewHolder>  {
    protected Context context;
    private LayoutInflater inflater;
    private ArrayList<ModelSpacialization> joblist   ;
    private String TYPE = "";
    private int count = 0 ;
    MultiCheckAdaptar.OnItemClickListener listener;

    public interface OnItemClickListener {
        void onClick(ModelSpacialization jobs, int pos , boolean type);
    }

    public MultiCheckAdaptar(Context context, ArrayList<ModelSpacialization> list, MultiCheckAdaptar.OnItemClickListener listener) {
        this.context = context;
        this.listener = listener;
        if (context != null) {
            inflater = LayoutInflater.from(context);
            this.joblist= new ArrayList<>();
            this.joblist.addAll(list);

         }
    }

    public MultiCheckAdaptar.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.layout_ckeck_item , parent, false);
        MultiCheckAdaptar.MyViewHolder holder = new MultiCheckAdaptar.MyViewHolder(view);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }


    @Override
    public void onBindViewHolder(@NonNull final MultiCheckAdaptar.MyViewHolder holder, final int position) {
        ModelSpacialization item = joblist.get(position);
        holder.txt_item.setText(item.getName());

        if(item.isCheck())
           holder.txt_item.setChecked(true) ;
         else
            holder.txt_item.setChecked(false) ;

        holder.txt_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   listener.onClick(item, position, false);
                }
        });
    }

    @Override
    public int getItemCount() {
        return joblist.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
       CheckBox txt_item ;
          public MyViewHolder(View itemView) {
            super(itemView);
              txt_item = itemView.findViewById(R.id.txt_item);

        }

    }
}

layout_check_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/txt_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/size_10"
        android:text="text"

        android:textSize="@dimen/size_16" />

</RelativeLayout>

dialog_dropdwon_recyle.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:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:text="TItle"
        android:id="@+id/tv_dialogTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="@color/primary"
        android:fontFamily="@font/poppins_medium"
        android:textSize="@dimen/size_16"

        tools:ignore="SpUsage" />

    <ImageButton
        android:id="@+id/btn_dialog_close"
        android:layout_width="@dimen/size_25"
        android:layout_height="@dimen/size_25"
        android:layout_alignParentEnd="true"
        android:layout_gravity="right"
        android:background="@drawable/btn_grey_transparent"
        android:elevation="1dp"
        android:padding="@dimen/size_5"
        android:src="@drawable/ic_close" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_dialog_close"
        android:paddingHorizontal="@dimen/size_5"
        android:orientation="vertical">

        <EditText
            android:id="@+id/edit_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Search..."
            android:visibility="gone" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dialog_list"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/size_10"
            android:scrollbars="none" />
        <Button
            android:id="@+id/btn_save"
            android:layout_width="match_parent"
            android:layout_height="@dimen/size_40"
            android:layout_marginHorizontal="@dimen/size_10"
            android:layout_marginTop="@dimen/size_10"
            android:layout_marginBottom="@dimen/size_10"
            android:background="@drawable/button_primary"
            android:fontFamily="@font/poppins_medium"
            android:text="Save"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="@dimen/size_15" />
    </LinearLayout>
</RelativeLayout>

in Activity

Dialog dg_industries = new Dialog(context);
        selected_spaci = new HashSet<>();
        industresList.addAll(industries2);
        selected_spaci = new HashSet<>();
        dg_industries.setContentView(R.layout.dailogbox_dwondwon_recycle);
        final RecyclerView indview = (RecyclerView) dg_industries.findViewById(R.id.dialog_list);
        final TextView title = (TextView) dg_industries.findViewById(R.id.tv_dialogTitle);
        title.setVisibility(View.VISIBLE);
        title.setText("Select  Specialization");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        indview.setLayoutManager(linearLayoutManager);
        adaptor = new MultiCheckAdaptar(context, industresList, new MultiCheckAdaptar.OnItemClickListener() {
            @Override
            public void onClick(ModelSpacialization jobs, int pos, boolean type) {
                jobs.setCheck(type);
                selected_spaci.add(jobs);
                Log.e("industries2", new Gson().toJson(industresList));
            }
        });

        dg_industries.setCancelable(false);
        dg_industries.show();