【问题标题】:get RadioGroup count in android在android中获取RadioGroup计数
【发布时间】:2014-03-10 06:35:43
【问题描述】:

我正在线性布局中创建多个(动态)无线电组。每个都有 4 个单选按钮。在那个linearLayout里面有一些TextViews

我的问题是在按下按钮时,我需要获取所有 radioGroup 以获取数据。

我尝试了以下方法,但得到了包括 textViews 在内的全部计数

LinearLayout ll=(LinearLayout)findViewById(R.id.questions_lay);
int childcount = ll.getChildCount();

有什么想法吗? 提前致谢

【问题讨论】:

    标签: android radio-button


    【解决方案1】:

    你可以使用

    public void getViewType(ViewGroup viewGroup)
    {
        for (int index = 0; index < viewGroup.getChildCount(); index++)
        {
            View childView = viewGroup.getChildAt(index);
    
            if (childView instanceof RadioGroup)
            {
                System.err.println("RadioGroup " + index);
                getViewType((ViewGroup) childView);
            }
            else if (childView instanceof RadioButton)
            {
                System.err.println("RadioButton " + index);
            }
            else if (childView instanceof TextView)
            {
                System.err.println("TextView " + index);
            }
        }
    }
    

    当您获得instanceOf RadioGroup 时,如果您想在该RadioGroup 中访问RadioButton,请再次调用getViewType

    【讨论】:

      【解决方案2】:

      看看这个tutorial,他们是如何通过“radio_button”获取数据的。

      例如要使用单选按钮获取数据,请使用:

      RadioButton rBtnUp = (RadioButton) findViewById(R.id.xml_file_forUPID);
      RadioButton rBtnDown = (RadioButton) findViewById(R.id.XML_file_forDOWNID);
      TextView txtCount = (TextView) findViewById(R.id.txtCountId);
            txtCount.setText(String.valueOf(count)); // Display initial count
      
           Button btnCount = (Button) findViewById(R.id.btnCountId);
            // Process the button on-click event
            btnCount.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                  if (rBtnUp.isChecked()) { // Counting up
                     count++;
                  } else if (rBtnDown.isChecked()) { // Counting down
                     count--;
                  }
                  txtCount.setText(String.valueOf(count));
               }
      

      【讨论】:

        【解决方案3】:

        以下代码用于 TextView。您可以将其用于任何类型的视图。

        //Recursively search for TextView in given ViewGroup
        public int getTextViewCount(ViewGroup viewGroup) {
        
            int count = 0;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View childView = viewGroup.getChildAt(i);
        
                if (childView instanceof TextView) {
                    count++;
                }
                if (childView instanceof ViewGroup) {
                    count += getTextViewCount((ViewGroup) childView);
                }
        
            }
            return count;
        
        }
        

        【讨论】:

          猜你喜欢
          • 2011-07-03
          • 1970-01-01
          • 1970-01-01
          • 2011-09-20
          • 1970-01-01
          • 1970-01-01
          • 2021-03-02
          • 2013-02-19
          • 1970-01-01
          相关资源
          最近更新 更多