2 回答
TA贡献1890条经验 获得超9个赞
您可以创建一个集合来存储单选按钮的检查状态,然后在加载集合时加载该集合getView。像这样 :
class YourAdapter : BaseAdapter,CompoundButton.IOnCheckedChangeListener
{
private Dictionary<int, bool> checkDictionary = new Dictionary<int, bool>();
int[] item; //raplace your own data
public MyAdapter(int[] value) //raplace your own data
{
item = value;
for (int i = 0; i < item.Length; i++)
{
checkDictionary.Add(i,false);
}
}
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 = position;
lblradio.Checked = checkDictionary[position];
lblradio.SetOnCheckedChangeListener(this);
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;
}
else
{
checkDictionary[i] = false;
}
}
NotifyDataSetChanged();
}
}
TA贡献1811条经验 获得超4个赞
您可以选择多个单选按钮的原因是它们不属于同一个单选按钮组。
引用自 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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
C#:
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.Orientation = Orientation.Horizontal;//or Orientation.VERTICAL
RadioButton rb = new RadioButton(this);// create the radiobutton
rg.AddView(rb);// add to radio group
- 2 回答
- 0 关注
- 104 浏览
添加回答
举报