2016-08-23 112 views
0

我用編程方式添加了複選框的小問題。當我點擊「發送按鈕」時,我不知道如何檢查選中哪個複選框。如何檢查複選框是否被選中?

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,但我真的不知道該怎麼做。我在想方法,它給我創建複選框的代碼,但仍不知道如何檢查複選框是否被選中。

回答

1

我知道你有很多複選框控件。因此,將它們添加到列表如下:

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

當你想知道哪些是檢查,你會做到這一點:

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

如何添加「for循環」複選框的屬性(文本,layoutparameters)到列表?我不知道這些複選框必須有多少。 – coldzero

+0

@coldzero這些複選框如何在您的表單中聲明?類字段或屬性? –

+0

它們是動態添加的,因爲我從url獲取json結構並將其轉換爲數據,以創建複選框。有時候有3個,但有時候有10或20個。 – coldzero

0

這是如何快速和骯髒的樣本生成動態cheboxes和retreive他們的狀態:

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); 

     } 
    } 

這是一個關於如何在最小的努力實現這個樣品。

相關問題