Multi Select Spinner Xamarin Android

时间:2018-02-13 14:39:26

标签: xamarin.android android-spinner

我正在使用微调器和arrayAdapter来填充微调器中的项目。我想让微调器多选,但我只选择一行。我在谷歌搜索,解决方案可用Java,但我不知道如何在Xamarin中实现它。

我的代码如下,

adapterList= new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemMultipleChoice);

spnMultiTest.Adapter = adapterList;

1 个答案:

答案 0 :(得分:1)

  

我想让微调器多选,但我只选择一行。我在谷歌搜索,解决方案有Java,但我不知道如何在Xamarin中实现它。

基本上我只是将代码从Android Spinner with multiple choice 翻译成Xamarin代码。我测试了它,它工作正常:

  1. MultiSpinner.cs:

    public interface MultiSpinnerListener
    {
        void onItemsSelected(bool[] selected);
    }
    public class MultiSpinner : Spinner, IDialogInterfaceOnMultiChoiceClickListener, IDialogInterfaceOnCancelListener
    {
        Context _context;
    
        private List<String> items;
        private bool[] selected;
        private String defaultText;
        private MultiSpinnerListener listener;
    
        public MultiSpinner(Context context) : base(context)
        {
            _context = context;
        }
    
        public MultiSpinner(Context context, IAttributeSet arg1) : base(context, arg1)
        {
            _context = context;
       }
    
        public MultiSpinner(Context context, IAttributeSet arg1, int arg2) : base(context, arg1, arg2)
        {
            _context = context;
        }
    
    
        public  void OnClick(IDialogInterface dialog, int which, bool isChecked)
        {
            if (isChecked)
                selected[which] = true;
            else
                selected[which] = false;
        }
    
        public override void OnClick(IDialogInterface dialog, int which)
        {
            dialog.Cancel();
        }
    
    
        public override bool PerformClick()
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(_context);
            builder.SetMultiChoiceItems(
                    items.ToArray(), selected, this);
    
            builder.SetPositiveButton("OK",this);
            builder.SetOnCancelListener(this);
            builder.Show();
            return true;
        }
    
    
        public void SetItems(List<String> items, String allText,
            MultiSpinnerListener listener)
        {
    
            this.items = items;
            this.defaultText = allText;
            this.listener = listener;
    
            // all selected by default
            selected = new bool[items.Count];
            for (int i = 0; i < selected.Length; i++)
                selected[i] = true;
            ArrayAdapter<string> adapter = new ArrayAdapter<string>(_context,Resource.Layout.simple_spinner_item,Resource.Id.tv_item,new string[] { allText });
            // all text on the spinner
            //ArrayAdapter<String> adapter = new ArrayAdapter<String>(_context,Resource.Layout.simple_spinner_item, new String[] { allText });
            Adapter = adapter;
        }
    
        public void OnCancel(IDialogInterface dialog)
        {
            Java.Lang.StringBuffer spinnerBuffer = new Java.Lang.StringBuffer();
            bool someUnselected = false;
            for (int i = 0; i < items.Count; i++)
            {
                if (selected[i] == true)
                {
                    spinnerBuffer.Append(items[i]);
                    spinnerBuffer.Append(", ");
                }
                else
                {
                    someUnselected = true;
                }
            }
            String spinnerText;
            if (someUnselected)
            {
                spinnerText = spinnerBuffer.ToString();
                if (spinnerText.Length > 2)
                    spinnerText = spinnerText.Substring(0, spinnerText.Length - 2);
            }
            else
            {
                spinnerText = defaultText;
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(_context,Resource.Layout.simple_spinner_item,Resource.Id.tv_item,new string[] { spinnerText });
            Adapter = adapter;
            if (listener != null)
            {
               listener.onItemsSelected(selected);
            }
    
        }
    
    
    }
    
  2. simple_spinner_item.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <TextView
        android:id="@+id/tv_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    </LinearLayout>
    
  3. <强>更新

    以下是使用此MultiSpinner的代码:

    Main.axml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <MultiSpinner.MultiSpinner
        android:id="@+id/mSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    </LinearLayout>
    

    MainActivity.cs

    public class MainActivity : Activity
    {
        MultiSpinner mSpinner;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            mSpinner = FindViewById<MultiSpinner>(Resource.Id.mSpinner);
            List<string> items = new List<string> {
                "Android",
                "iOS",
                "UWP"
            };
    
            mSpinner.SetItems(items, "AllText", null);
        }
    }