簡體   English   中英

所有單選按鈕都在一個無線電組,android下被選中

[英]All radio buttons are getting selected under a radio group, android

我是android的新手,我有一個像這樣的xml:

<RadioGroup
        android:id="@+id/cardRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="45dp" android:layout_alignParentLeft="true" android:layout_marginLeft="35dp"
        android:layout_alignTop="@+id/cardEditText">
</RadioGroup>

我打算根據自己的數據模型,使用我的代碼為這個xml充氣,以生成任意數量的單選按鈕。

我使用的代碼是(在for循環中):

        LayoutInflater inflater = this.getLayoutInflater();
        View currentView = inflater.inflate(R.layout.card_payment_content_node,null);

        RadioGroup rg = (RadioGroup) currentView.findViewById(R.id.cardRadioGroup);
        RadioButton rb =  new RadioButton(this);
        //set radiobutton properties
        rb.setText(entry.getKey());
        rb.setId(++radioButtonIdCounter);

        rg.addView(rb);

        //add to view
        parent.addView(currentView);

這是按預期工作的。 但問題是我可以在設備上一次選擇多個單選按鈕。

我不確定為什么會這樣。 我在哪里弄錯了?

提前致謝。

您所有的按鈕都具有相同的ID。 這就是為什么他們如此統一地行動。

您只需要膨脹一次布局。 您必須通過識別cardRadioGroup以編程方式添加按鈕,然后在代碼中循環,為所有按鈕生成唯一ID。

要將單選按鈕添加到組,請使用RadioGroup.addview()

要創建單選按鈕,請使用RadioButton的構造函數之一,並根據需要設置其他屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM