如何检查单选按钮以及如何控制在列表视图中仅选择一个单选按钮

我试图将我的数据添加到列表视图。我正在使用适配器。列表视图内部有单选按钮和一些字段。


class StocktakeEditViewAdaptor : BaseAdapter<Model.FileRecord>

{

private Dictionary<int, bool> checkDictionary = new Dictionary<int, bool>();




public override View GetView(int position, View convertView, ViewGroup parent)

    {

        var item = items[position];

        View view = convertView;


        //if (view == null)

        {

            view = context.LayoutInflater.Inflate(Resource.Layout.StockTakeEditDetailList, null);

            view.DuplicateParentStateEnabled = true;



            createdview.Add(view);

            RadioButton lblradio = view.FindViewById<RadioButton>(Resource.Id.lblradio);

            lblradio.Tag = item.FileRecord_ID + ":" + item.ST_filename + ":" + item.ST_BinLoc;

lblradio.Checked = checkDictionary[position];   //add position here

            lblradio.SetOnCheckedChangeListener(null);

            lblradio.SetOnCheckedChangeListener(new CheckedChangeListener(this.context));


            view.FindViewById<TextView>(Resource.Id.txtLineNo).Text = item.FileRecord_ID.ToString();  //my field in adapter.

            view.FindViewById<TextView>(Resource.Id.txtbinloc).Text = item.ST_BinLoc.ToString();

            view.FindViewById<TextView>(Resource.Id.txtBarcodett).Text = item.ST_Barcode.ToString();

            view.FindViewById<TextView>(Resource.Id.txtQtytt).Text = item.ST_Qty.ToString();



            if (!view.HasOnClickListeners)

                view.Click += View_LongClick;

            view.RefreshDrawableState();

        }

        return view;

    }


        public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)

        {

            for (int i = 0; i < checkDictionary.Count(); i++)

            {

                if (i == (int)buttonView.Tag)

                {

                    checkDictionary[i] = true;   //error on here

                }

                else

                {

                    checkDictionary[i] = false;  //error on here

                }

            }


        }

我可以在列表视图中显示我的记录。但是单选按钮可以选择多个。我不想选择多条记录。


蛊毒传说
浏览 105回答 2
2回答

当年话下

您可以创建一个集合来存储单选按钮的检查状态,然后在加载集合时加载该集合getView。像这样 :class YourAdapter : BaseAdapter,CompoundButton.IOnCheckedChangeListener&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private Dictionary<int, bool> checkDictionary = new Dictionary<int, bool>();&nbsp; &nbsp; &nbsp; &nbsp; int[] item;&nbsp; //raplace your own data&nbsp; &nbsp; &nbsp; &nbsp; public MyAdapter(int[] value) //raplace your own data&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < item.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkDictionary.Add(i,false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }public override View GetView(int position, View convertView, ViewGroup parent)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;var item = items[position];&nbsp; &nbsp; &nbsp; &nbsp;View view = convertView;&nbsp; &nbsp; //if (view == null)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; view = context.LayoutInflater.Inflate(Resource.Layout.StockTakeEditDetailList, null);&nbsp; &nbsp; &nbsp; &nbsp; view.DuplicateParentStateEnabled = true;&nbsp; &nbsp; &nbsp; &nbsp; createdview.Add(view);&nbsp; &nbsp; &nbsp; &nbsp; RadioButton lblradio = view.FindViewById<RadioButton>(Resource.Id.lblradio);&nbsp; &nbsp; &nbsp; &nbsp; lblradio.Tag = position;&nbsp; &nbsp; &nbsp; &nbsp; lblradio.Checked = checkDictionary[position];&nbsp; &nbsp; &nbsp; &nbsp; lblradio.SetOnCheckedChangeListener(this);&nbsp; &nbsp; &nbsp; &nbsp; view.FindViewById<TextView>(Resource.Id.txtLineNo).Text = item.FileRecord_ID.ToString();&nbsp; //my field in adapter.&nbsp; &nbsp; &nbsp; &nbsp; view.FindViewById<TextView>(Resource.Id.txtbinloc).Text = item.ST_BinLoc.ToString();&nbsp; &nbsp; &nbsp; &nbsp; view.FindViewById<TextView>(Resource.Id.txtBarcodett).Text = item.ST_Barcode.ToString();&nbsp; &nbsp; &nbsp; &nbsp; view.FindViewById<TextView>(Resource.Id.txtQtytt).Text = item.ST_Qty.ToString();&nbsp; &nbsp; &nbsp; &nbsp; if (!view.HasOnClickListeners)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.Click += View_LongClick;&nbsp; &nbsp; &nbsp; &nbsp; view.RefreshDrawableState();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < checkDictionary.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (i == (int) buttonView.Tag)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkDictionary[i] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkDictionary[i] = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NotifyDataSetChanged();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}

波斯汪

您可以选择多个单选按钮的原因是它们不属于同一个单选按钮组。引用自 Android Radiobutton官方文档。要创建每个单选按钮选项,请在布局中创建一个 RadioButton。但是,由于单选按钮是互斥的,您必须在 RadioGroup 中将它们组合在一起。通过将它们组合在一起,系统可以确保一次只能选择一个单选按钮。这个怎么做?RadioGroup 就像 Radiobuttons 的视图组,因此您可以像使用它一样使用它XML:<?xml version="1.0" encoding="utf-8"?><RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><RadioButton android:id="@+id/radio_pirates"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:text="@string/pirates"&nbsp; &nbsp; android:onClick="onRadioButtonClicked"/><RadioButton android:id="@+id/radio_ninjas"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:text="@string/ninjas"&nbsp; &nbsp; android:onClick="onRadioButtonClicked"/></RadioGroup>C#:RadioGroup rg = new RadioGroup(this); //create the RadioGrouprg.Orientation = Orientation.Horizontal;//or Orientation.VERTICALRadioButton rb = new RadioButton(this);// create the radiobuttonrg.AddView(rb);// add to radio group&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP