如何获取在 android 中动态创建的已选中单选按钮的 ID

How to get checked radio button's id created dynamically in android

在我的 activity 中,我正在创建一个动态无线电组。我在Sqlite数据库中有一些问题和答案,在activity中检索它们,然后在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--;
            }
        }

你创建 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]);
            }

像这样,您可以设置 Id,或者如果您愿意,也可以将标签添加到进程中。

                rb.setTag(SomeObject)

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

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

Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.

标识符不必是唯一的,但如果它不是唯一的,findViewById将return第一次出现,所以你可以使用静态方法View.generateViewId添加在API 17级.

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

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

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