没有AlertDialog的android中的多选微调器

Posted

技术标签:

【中文标题】没有AlertDialog的android中的多选微调器【英文标题】:Multi selection spinner in android without AlertDialog 【发布时间】:2014-08-22 19:22:34 【问题描述】:

我想要与此链接相同的 https://www.gorecess.com/ first spinner 。带有复选框的android中的多选微调器。在下拉列表中显示微调器。谁知道答案...

【问题讨论】:

您可以使用自定义布局。 你知道任何链接只是参考我 【参考方案1】:

            <com.extra.MultiSelectionSpinner
                android:id="@+id/input1"
                android:layout_
                android:layout_
                android:layout_margin="2dp" />

       MultiSelectionSpinner spinner=(MultiSelectionSpinner)findViewById(R.id.input1);

       List<String> list = new ArrayList<String>();
          list.add("List1");
          list.add("List2");
          spinner.setItems(list);

【讨论】:

谢谢,如何设置占位符? 请告诉我如何设置占位符?【参考方案2】:

多选微调器:

1- 在你自己的 xml 中创建一个微调器,像这样

 <Spinner
    android:id="@+id/mySpinner"
    android:layout_
    android:layout_
    android:layout_alignParentRight="true"
    android:layout_marginTop="1dp"/>

2-像这样为微调器创建一个自定义 dapter:

public class AdapterTagSpinnerItem extends ArrayAdapter<TagListSimpleSearch>

    private LayoutInflater mInflater;
    private List<TagListSimpleSearch> listState;
    public Spinner mySpinner = null;

    public AdapterTagSpinnerItem(Context context, int resource, List<TagListSimpleSearch> objects, Spinner mySpinner)
    
        super(context, resource, objects);
        this.listState = objects;
        this.mySpinner = mySpinner;
        mInflater = LayoutInflater.from(context);
    

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    
        return getCustomView(position, convertView, parent);
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    
        return getCustomView(position, convertView, parent);
    

    public View getCustomView(final int position, View convertView, ViewGroup parent)
    
        String text = "";
        final ViewHolder holder;
        if (convertView == null)
        
            holder = new ViewHolder();
            mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.spinner_item, null, false);
            holder.mTextView = convertView.findViewById(R.id.tvSpinnerItem);
            convertView.setTag(holder);
        
        else
        
            holder = (ViewHolder) convertView.getTag();
        
        /**
         * check position , if position is zero we put space on top of list of spinner
         */
        if ((position == 0))
            text = oneSpace;
        /**
         * check position , if position is one we put cross mark before text to show that position used to be for clear all selected items on spinner
         */
        else if ((position == 1))
            text = "  " + String.valueOf((char) crossMarkAroundBox) + " " + listState.get(position).getTagText();
        /**
         * check position , if position is two we put check mark before text to show that position used to be for select all items on spinner
         */         
        else if ((position == 2))
            text = "  " + String.valueOf((char) tikMarkAroundBox) + " " + listState.get(position).getTagText();
        /**
         * check position , if position is bigger than two we have to check that position is selected before or not and put check mark or dash before text 
         */         
        else
        
            if (listState.get(position).isSelected())
            
                text = "  " + String.valueOf((char) tikMark) + " " + listState.get(position).getTagText();
            
            else
            
                text = "  " + String.valueOf(dash) + " " + listState.get(position).getTagText();
            
        
        holder.mTextView.setText(text);
        holder.mTextView.setTag(position);
        holder.mTextView.setOnClickListener(new View.OnClickListener()
        
            @Override
            public void onClick(View v)
            
                /**
                 * if you want open spinner after click on text for first time we have to open spinner programmatically 
                 */
                mySpinner.performClick();
                int getPosition = (Integer) v.getTag();
                listState.get(getPosition).setSelected(!listState.get(getPosition).isSelected());
                notifyDataSetChanged();

                /**
                 * if clicked position is one 
                 * that means you want clear all select item in list 
                 */             
                if (getPosition == 1)
                
                    clearList();
                
                /**
                 * if clicked position is two 
                 * that means you want select all item in list 
                 */
                else if (getPosition == 2)
                
                    fillList();
                
            
        );
        return convertView;
    


    /**
     * clear all items in list
     */
    public void clearList()
    
        for (TagListSimpleSearch items : listState)
        
            items.setSelected(false);
        
        notifyDataSetChanged();
    

    /**
     * select all items in list
     */
    public void fillList()
    
        for (TagListSimpleSearch items : listState)
        
            items.setSelected(true);
        
        notifyDataSetChanged();
    

    /**
     * view holder
     */
    private class ViewHolder
    
        private TextView mTextView;
    

3- 现在你必须为适配器创建对象

public class TagListSimpleSearch 

    private String TagId;
    private String TagText;
    private boolean selected;

    public String getTagId() 
        return TagId;
    

    public void setTagId(String TagId) 
        this.TagId = TagId;
    

    public String getTagText() 
        return TagText;
    

    public void setTagText(String tagText) 
        TagText = tagText;
    

    public boolean isSelected()
    
        return selected;
    

    public void setSelected(boolean selected)
    
        this.selected = selected;
    

您的活动中的 4 填充微调器适配器

public static String oneSpace =" ";
public static int tikMark =0X2714;
public static int crossMark =0X2715;
public static int tikMarkAroundBox =0X2611;
public static int crossMarkAroundBox =0X274E;
public static String dash ="-";
private Spinner mySpinner;

mySpinner= (Spinner) findViewById(R.id.mySpinner);
List<TagListSimpleSearch> tagsNames = new ArrayList<>();
TagListSimpleSearch tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("0");
tagSpecific.setTagText(oneSpace);
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("1");
tagSpecific.setTagText("select All Items");
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("2");
tagSpecific.setTagText("remove All Items");
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("0");
tagSpecific.setTagText("Item 0");
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("1");
tagSpecific.setTagText("Item 1");
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("2");
tagSpecific.setTagText("Item 2");
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("3");
tagSpecific.setTagText("Item 3");
tagsNames.add(tagSpecific);

final AdapterTagSpinnerItem adapterTagSpinnerItem = new AdapterTagSpinnerItem(this, 0, tagsNames,mySpinner);
mySpinner.setAdapter(adapterTagSpinnerItem);

【讨论】:

你能发布你的 spinner_item 布局【参考方案3】:

我已经使用 AlertDialog 在 android 中实现了一个多选微调器

检查以下代码

 public void classesSelect()


    regclassvalue.setText("");


    AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);


    builder.setTitle("Select Services");

    final int array[] = new int[serarray.length];

    check = new boolean[serarray.length];

    for (int k = 0; k < serarray.length; k++) 

        check[k] = false;

    

    final List<String> classlist = Arrays.asList(serarray);

    final List<Integer> classidlist = Arrays.asList(classidarray);

// 这是主要部分,我们使用 serarray 设置多选监听器并检查

    builder.setMultiChoiceItems(serarray, check, new                   DialogInterface.OnMultiChoiceClickListener() 

        @Override

        public void onClick(DialogInterface dialog, int which, boolean isChecked) 

            check[which] = isChecked;

        

    );

    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() 

        @Override

        public void onClick(DialogInterface dialog, int which) 

            regclassvalue.setText("");

            classids = "";

            int io = 0;

            for (int ii = 0; ii < check.length; ii++) 

                boolean checked = check[ii];

                if (checked) 

                    array[io] = ii;

                    io++;

                

            

            for (int k = 0; k < io; k++) 

                if (io == 1) 

                    classids = classids + classidlist.get(array[k]);

                    regclassvalue.setText(regclassvalue.getText() + classlist.get(array[k]));

                 else if (k == io - 1) 

                    classids = classids + classidlist.get(array[k]);

                    regclassvalue.setText(regclassvalue.getText() + classlist.get(array[k]));

                 else 

                    classids = classids + classidlist.get(array[k]) + ",";

                    regclassvalue.setText(regclassvalue.getText() + classlist.get(array[k]) + " ,");

                

            

            //Toast.makeText(RegisterActivity.this,classids,Toast.LENGTH_LONG).show();

        

    );

    builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() 

        @Override

        public void onClick(DialogInterface dialog, int which) 

            dialog.cancel();



        

    );

    AlertDialog dialog = builder.create();

    dialog.show();




更多信息请点击here

【讨论】:

以上是关于没有AlertDialog的android中的多选微调器的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio基础使用多选对话框AlertDialog

Android中的普通对话框单选对话框多选对话框带Icon的对话框以及自定义Adapter和自定义View对话框详解

Android中的AlertDialog使用示例四(多项选择确定对话框)

Android Dialog 普通对话框 单选对话框 多选对话框

android 4.0的多选列表使用

Visual Studio 中的多选?