【问题标题】:How to get checked radio button's id created dynamically in android如何在android中动态创建选中的单选按钮的ID
【发布时间】:2015-05-14 07:18:18
【问题描述】:

在我的活动中,我正在创建一个动态无线电组。我在 Sqlite 数据库中有一些问题和答案,在活动中检索它们,然后在 RadioGroup 中设置。这工作正常。但在那之后,我想通过用户获取所选单选按钮的所有 ID 以将它们存储在数据库中。一般来说,当我们有选项的 ID 时,我们会这样做:

            id1=rdgroup1.getCheckedRadioButtonId();
            que1=(RadioButton)findViewById(id1);
            ans1=que1.getId()+""; // so here I will get radio button's ID.

所以我的问题是,我将如何获得选定单选按钮的 ID。这是我动态创建无线电组的代码。

     LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
        c1=db.rawQuery("SELECT * FROM QueTable WHERE AgeGroup='10-20'", null);
        c1.moveToFirst();
        int i = c1.getCount();
        if(i > 0)
        {
            Log.w("START", "start");
            while (i > 0)
            {
                TextView title = new TextView(this);
                questions = c1.getString(1);
                title.setText(questions);
                title.setTextColor(Color.BLACK);
                mLinearLayout.addView(title);
                // create radio button

                answers=c1.getString(2);
                String[] answer = answers.split(",");
                rb = new RadioButton[5];
                rg = new RadioGroup(this);
                rg.setOrientation(RadioGroup.VERTICAL);

                int k = answer.length;

                for (int j = 0; j < k; j++)
                {
                    rb[j] = new RadioButton(this);
                    rg.addView(rb[j]);
                    rb[j].setText(answer[j]);
                }
                mLinearLayout.addView(rg);
                c1.moveToNext();
                i--;
            }
        }

【问题讨论】:

  • how will I get selected radio button's ID 但您正在使用que1.getId() 获取 id 那么有什么问题?
  • @ρяσѕρєяK 问题是我正在以编程方式创建无线电组。所以我不知道任何单选按钮的 ID。请参阅动态无线电组代码的第二部分。当我们在 xml 布局中使用拖放创建它们时使用 que1.getId(),然后在 xml 中为它们提供 id,如 android:id...
  • 那么有什么问题使用id1=rg.getCheckedRadioButtonId();que1=(RadioButton)findViewById(id1);ans1=que1.getId()+""; ??
  • @ρяσѕρєяK 无论你说什么都是静态单选按钮而不是动态。所以我知道那个静态单选按钮的 ID,但不知道动态添加的按钮。
  • @Ruchir Tarawat : 您可以在代码中使用 setid 方法设置单选按钮 id

标签: java android sqlite android-sqlite android-radiogroup


【解决方案1】:

您创建 RadioButton 集及其 ID 的位置

  for (int j = 0; j < k; j++)
            {
               RadioButton rb = new RadioButton(this);
                rb.setId(Somenumber + j)
                 rb[j] = rb;
                rg.addView(rb[j]);
                rb[j].setText(answer[j]);
            }

像这样你可以 setId 或者如果你想你可以添加标签来处理。

                rb.setTag(SomeObject)

【讨论】:

    【解决方案2】:

    您需要为以编程方式创建的RadioButton 设置一个ID,以便使用findViewById 找到它,如您在第一个代码中所示。

    要为RadioButton 设置ID,您可以使用View.setId()。文档说:

    设置此视图的标识符。标识符不必是 在此视图的层次结构中是唯一的。标识符应该是正数 号码。

    标识符不必是唯一的,但如果不是唯一的,findViewById 将返回第一次出现,因此您可以使用 API 级别 17 上添加的静态方法 View.generateViewId

    如果您使用较低的 API,您可能需要编写自己的函数来查找合适的(未使用的)ID。你可以看看Android: View.setID(int id) programmatically - how to avoid ID conflicts?

    【讨论】:

    • 你的答案很好..设置单选按钮的 id 但是 radioGroup 呢。我的意思是,它将如何检测选择了哪个 radioGroup 的单选按钮。我有 20 个问题,每个问题有 4 个选项。所以用户将点击所有选项。要获取单选按钮的 id,我还需要 radioGroup 的 id。
    • @Ruchir Tarawat 您也可以为您的RadioGroup 设置 ID。此外,您可以使用setTag()RadiogroupRadioButton 设置标签并检查标签(根据文档“标签可用于标记其层次结构中的视图”)。您还可以创建某种结构(Map 可以做到这一点)将每个 RadioButton 与您要存储在数据库中的密钥相关联。
    【解决方案3】:

    好的..所以最后我得到了解决方案。我将在这里分享完整的代码,它将从数据库中动态创建单选组和单选按钮。下面是代码:

    private static final int RB_ID = 100;
    int k=0,len=0,r=0,p = 0,q=0, len = 0;
    
     LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
     c1 = db.rawQuery("SELECT * FROM QueMotion WHERE AgeGroup ='"+Age+"' LIMIT 5", null);
                c1.moveToFirst();
                int i = c1.getCount();
                rg = new RadioGroup[i];
                rb = new RadioButton[i*5];
                if (i > 0) {
                    Log.w("START", "start");
                    while (i > 0)
                    {
                        TextView title = new TextView(this);
                        questions = c1.getString(1);// retrive from database
                        title.setText(questions);
                        title.setTextColor(Color.BLACK);
                        title.setTypeface(null, Typeface.BOLD);
                        title.setPadding(0, 0, 0, 10);
                        mLinearLayout.addView(title);
                        answers = c1.getString(2);// retrive from database
                        String[] answer = answers.split(",");// will create options for radio button.
                        rg[p] = new RadioGroup(this);
                        rg[p].setOrientation(RadioGroup.VERTICAL);
                        len=len+answer.length;
                        for (int j = 0; j < answer.length; j++)
                        {
                                rb[q] = new RadioButton(this);
                                rg[p].addView(rb[q]);
                                rb[q].setId(k + RB_ID);
                                rb[q].setText(answer[j]);
                                k++;
                                q++;
                        }
                        mLinearLayout.addView(rg[p]);
                        c1.moveToNext();
                        i--;
                        p++;
                    }
    
        //Submit button click
         submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    if (rg[0].getCheckedRadioButtonId() == -1)
                    {
                        Log.e("Nothing selected", "Nothing selected");
                    }
                    else
                    {
                        for (r = 0; r < len; r++) {
                            if (rb[r].isChecked()) {
                                RadioButton id = (RadioButton) findViewById(rb[r].getId());
                                String radioText = id.getText().toString();
                                c3.moveToFirst();
                                Log.e("RadioString", radioText);
                            } else {
                                Log.e("RadioString", "nothing");
                            }
                        }
                    }
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2010-09-13
      相关资源
      最近更新 更多