【问题标题】:multi select checkable dropdown check boxes in alert dailog in androidandroid中的alertdialog中的多选复选框下拉复选框
【发布时间】:2016-02-01 22:43:45
【问题描述】:

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

【问题讨论】:

  • 你的意思是你想要一个在警报对话框上显示另一个下拉列表的微调器吗?这听起来像是一个非常奇怪的 UI。
  • 不,我的意思是微调器之类的功能,但带有多选复选框。

标签: android checkbox android-alertdialog


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我无法使用包含复选框的下拉功能创建自定义布局。
    • 使用这个答案,作者方便地写了一个多选微调器,你可以使用它。 stackoverflow.com/questions/5015686/…
    • 我用同样的方法得到了我需要的东西。谢谢。
    【解决方案3】:

    使用下面的代码:-

    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();
    

    【讨论】:

    • 它与我获得的功能相同,但我需要在下拉菜单中使用此功能,即复选框应显示为下拉菜单。
    • 然后简单地制作一个自定义对话框。
    【解决方案4】:

    如你所见,在 AlertDialog 中无法获取点击事件。

    您问题的最佳解决方案。只需包含自定义布局并在您的活动中获取 Alertdialog Click 事件。

    1. 创建您想要设计的 XML 布局
    2. 通过 SetContentView() 将该 XML 布局包含在您的活动中
    3. 通过您的活动获取点击事件。

    请查看此链接以获取参考http://www.mkyong.com/android/android-custom-dialog-example/

    快乐编码:)

    【讨论】:

      【解决方案5】:

      对于自定义复选框下拉对话框,首先我们需要创建 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>
      

      在活动中

      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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        • 2017-05-14
        • 2019-11-19
        • 2018-05-09
        • 1970-01-01
        相关资源
        最近更新 更多