【问题标题】:Xamarin/C# Select only one Radio Button from custom list viewXamarin/C# 从自定义列表视图中仅选择一个单选按钮
【发布时间】:2018-01-03 01:23:35
【问题描述】:

我正在使用 Xamarin 创建我的应用程序。我有一个自定义 ListView atm。里面有单选按钮。这些单选按钮是可点击的,但每一个都是(多个)。

我想要的结果应该如下所示:当时只能检查 1 个。有一些 Java 解决方案,但它们根本没有帮助我。提前致谢。

这就是我创建列表视图的方式

        mListView = FindViewById<Android.Widget.ListView> (Resource.Id.listView1);

        mItems = new List<Artikel>();
        mItems.Add(new Artikel() { Name = "X", Amount = 50 });
        mItems.Add(new Artikel() { Name = "Y", Amount = 300 });
        mItems.Add(new Artikel() { Name = "Z", Amount = 80 });
        mItems.Add(new Artikel() { Name = "A", Amount = 174 });

        MyListViewAdapter adapter = new MyListViewAdapter(this, mItems);
        mListView.Adapter = adapter;

Artikel.cs

    class Artikel
    {
     public string Name { get; set; }
     public int Amount { get; set; }
    }

MyListViewAdapter.cs

    private List<Artikel> mItems;
    private Context mContext;


    public MyListViewAdapter(Context context, List<Artikel> items)
    {
        mItems = items;
        mContext = context;
    }

    public override int Count
    {
        get { return mItems.Count; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override Artikel this[int position]
    {
        get { return mItems[position]; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        if (row == null)
        {
            row = LayoutInflater.From(mContext).Inflate(Resource.Layout.listview_row, null, false);
        }
        TextView txtName = row.FindViewById<TextView>(Resource.Id.Name);
        txtName.Text = mItems[position].Name;

        TextView txtamount = row.FindViewById<TextView>(Resource.Id.Amount);
        txtamount.Text = mItems[position].Amount.ToString();


        return row;  
    }

提前致谢!

【问题讨论】:

  • 我看不到您在 ListView 中添加单选按钮的位置。你能用那个代码更新你的帖子吗? .通常要在 Android 中单独选择 Radiobutton,您需要使用 RadioGroup。
  • 我在列表视图的自定义布局中添加了单选按钮。它将如何与 RadioGroup 一起工作,您能解释一下吗?

标签: c# listview xamarin radio-button baseadapter


【解决方案1】:

正如我在 cmets 中所说,当您希望您的 RadioButtons 是独占的(仅根据上下文选择一个)时,您可以将它们包装在 RadioGroup 中。

想象一下这是 ListView 项的自定义布局。

<?xml version="1.0" encoding="utf-8"?>
<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/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView android:id="@+id/tvAmount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />    

    <RadioGroup android:layout_height="wrap_content" android:layout_width="match_parent">
        <RadioButton 
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:id="@+id/rb1"
            android:text="Option 1" />
        <RadioButton 
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:id="@+id/rb2"
            android:text="Option 2" />
        <RadioButton 
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:id="@+id/rb3"
            android:text="Option 3" />
        <RadioButton 
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:id="@+id/rb4"
            android:text="Option 4" />
    </RadioGroup>    
</LinearLayout>

通过添加RadioGroup,用户将能够从每个ListView 项目中仅选择一个RadioButton

希望这会有所帮助。-

【讨论】:

  • 不幸的是它没有用。问题是 Listview 的每一行都获得了所有的单选按钮。
  • 该代码正是应该做的。因此,现在根据您的评论,我了解您希望在整个 ListView 项目中选择单个 RadioButton。这是正确的吗?
猜你喜欢
  • 2013-04-12
  • 2013-02-22
  • 2014-03-13
  • 2019-02-16
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
相关资源
最近更新 更多