如何检查是否选中了复选框?

时间:2016-08-23 09:47:25

标签: c# checkbox xamarin.android

我没有以编程方式添加复选框的问题。当我点击“发送按钮”时,我不知道如何选中选中的复选框。

layout.RemoveAllViewsInLayout();

                    CheckBox _Options = new CheckBox(Activity);
                    ScrollView _Scroll = new ScrollView(Activity);
                    _Scroll.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
                    LinearLayout _LScroll = new LinearLayout(Activity);
                    _LScroll.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
                    _LScroll.Orientation = Orientation.Vertical;
                    _LScroll.SetGravity(GravityFlags.CenterHorizontal);

                    //_Scroll.AddView(_LScroll);
                    Button _Send = new Button(Activity);
                    _Send.Text = "Wyślij";
                    _Send.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);

                    for (int i = 0; i < _Item.options.Length; i++)
                    {
                        _Options.Text = _Item.options[i];
                        _Options.Id = i;
                        _Options.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
                        _LScroll.AddView(_Options);

                    }

                    _Send.Click += delegate
                    {
                        _MultiAnswer._QuestionId = _Item.id;

                        for(int i = 0; i < _Item.options.Length; i++)
                        {
                            if (_Options.Selected == true)
                            {
                                _MultiAnswer._AnwserOptionIds.SetValue(i + 1, i);
                            }
                        }

                        output = JsonConvert.SerializeObject(_MultiAnswer);
                        SendJson(_Url, DataCache._Login, output);

                        SetLayout(layout, btn);
                    };

                    _Scroll.AddView(_LScroll);
                    layout.AddView(_Scroll);
                    layout.AddView(_Send);

我会尝试使用复选框的ID,但我真的不知道该怎么做。我正在考虑方法,它为我提供了创建复选框的代码,但仍然不知道如何选中是否选中了复选框。

2 个答案:

答案 0 :(得分:1)

我知道你有很多复选框控件。所以将它们添加到列表中,如下所示:

List<Checkbox> checkboxes = new List<Checkbox>
{
    chk1, chk2, chk3
};

如果您想知道哪些已检查,您将执行以下操作:

IEnumerable<Checkbox> checkedCheckboxes = checkboxes.Where(chk => chk.Checked);

答案 1 :(得分:0)

这是关于如何生成动态chebox并检索其状态的快速而肮脏的示例:

public class MainActivity : Activity
    {

        public class MyItem
        {
            public string[]  options { get; set; }
            public int id { get; set; }
        }
        public class MyMultiAnswer
        {
            public int _QuestionId { get; set; }
        }

        private List<CheckBox> _chkList = new List<CheckBox>();

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            var _Item = new MyItem() { options =new string [] { "aaa", "bbb", "ccc" }, id=0 };
            var _MultiAnswer = new MyMultiAnswer() { _QuestionId = 0 };



            ScrollView _Scroll = new ScrollView(this);
            _Scroll.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            LinearLayout _LScroll = new LinearLayout(this);
            _LScroll.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            _LScroll.Orientation = Orientation.Vertical;
            _LScroll.SetGravity(GravityFlags.CenterHorizontal);

            TextView txView = new TextView(this);

            //_Scroll.AddView(_LScroll);
            Button _Send = new Button(this);
            _Send.Text = "test";
            _Send.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);

            for (int i = 0; i < _Item.options.Length; i++)
            {
                CheckBox _Options = new CheckBox(this);
                _chkList.Add(_Options);
                _Options.Text = _Item.options[i];
                _Options.Id = i;
                _Options.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
                _LScroll.AddView(_Options);
            }

            _Send.Click += delegate
            {
                _MultiAnswer._QuestionId = _Item.id;
                string strChkIds = "";

                foreach (var chk in _chkList.Where(c => c.Checked))
                {
                    //_MultiAnswer._AnwserOptionIds.SetValue(_Options.Id + 1, _Options.Id);
                    //do something
                    strChkIds += " - " + chk.Id;
                }

                // or

                for (int i = 0; i < _Item.options.Length; i++)
                {
                    if (_chkList[i].Checked == true)
                    {
                        //_MultiAnswer._AnwserOptionIds.SetValue(i + 1, i);
                        //do something
                    }
                }

                //output = JsonConvert.SerializeObject(_MultiAnswer);
                //SendJson(_Url, DataCache._Login, output);

                //SetLayout(layout, btn);
                txView.Text = "selected ids " + strChkIds;
            };

            _Scroll.AddView(_LScroll);
            _LScroll.AddView(_Send);
            _LScroll.AddView(txView);

            // Set our view from the "main" layout resource
            SetContentView(_Scroll);

        }
    }

这是一个关于如何以最小的努力实现这一目标的示例。