2016-07-28 19 views
3

我已經像下面那樣動態地添加了單選按鈕。如何獲取動態添加的單選按鈕的選定索引

for (int i = 0; i< typeArrayList.size(); i++) { 
        radioButtons[i] = new RadioButton(MainActivity.this); 
        radioButtons[i].setId(i); 
        radioButtons[i].setText(typeArrayList.get(i).toString()); 
        if(i==0) { 
         radioButtons[i].setChecked(true); 
        } 
        typeLayout.addView(radioButtons[i]); 
       } 

點擊時有一個按鈕調用一個方法,動態添加的單選按鈕的選定項目(文本)應該通過該方法。如何獲取動態添加的單選按鈕的選定單選按鈕文本?

+0

您是否檢查我們的解決方案 –

回答

0

您已將所有您創建的RadioButtons動態存儲在radioButtons array中。
所以,如果你想知道哪個單選按鈕被選中,你可以循環所有這些陣列,並檢查各RadioButton

for (int i = 0; i< radioButtons.size(); i++) { 
    if(radioButtons[i].isChecked()){ 
     // selected radio button is here 
     String text = radioButtons[i].getText(); 
    } 
} 
0

這可以通過使用標籤可以簡單地實現。

雖然添加了RadioButton設置了標記該特定對象。在你的情況,設置標籤爲單選按鈕的文字,

radioButtons[i].setTag(typeArrayList.get(i).toString()); 

這樣,所有的單選按鈕都會有一個標籤與之關聯文本。

現在無論何時選擇一個特定的單選按鈕,只需獲取標籤,該標籤將爲您提供與其相關的文本。

String text = (String) selectedRadioButton.getTag(); 

希望它有幫助。

0

首先,你必須得到你的看法的所有孩子。然後檢查這個視圖是否是RadioButton。最後檢查哪個按鈕被選中。

int childcount = typeLayout.getChildCount(); 
for (int i=0; i < childcount; i++){ 
     View view = typeLayout.getChildAt(i); 
     if (view instanceof RadioButton) { 
      if(((RadioButton)view).isChecked()) { 
       RadioButton yourCheckedRadioButton = (RadioButton) typeLayout.getChildAt(i); // this is your checked RadioButton 
      } 
     } 
} 
0

@Komali Shirumavilla

添加這些動態單選按鈕單選組 所以添加以下代碼中的行

RadioGroup rg = new RadioGroup(this); //create the RadioGroup 
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL 
for (int i = 0; i< typeArrayList.size(); i++) { 
radioButtons[i] = new RadioButton(MainActivity.this); 
radioButtons[i].setId(i); 
radioButtons[i].setText(typeArrayList.get(i).toString()); 
if(i==0) { 
    radioButtons[i].setChecked(true); 
} 
rg.addView(radioButtons[i]); // add dynamic radio buttons to radio group 
} 
typeLayout.addView(rg); // add radio group to view 

現在設置setOnCheckedChangeListenerRadioGroup

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       switch(checkedId) { 
        RadioButton btn = (RadioButton)findViewById(checkedId); 
        Log.d("Your selected radio button id",btn.getText()); 
       } 
      } 
     }); 
1

它對s很重要等你的單選按鈕ID添加按鈕時,收音機組。

RadioButton rb = new RadioButton(context); 
rb.setText(option); 
rb.setId(rb.hashCode()); 
radioGroup.addView(rb); 
radioGroup.setOnCheckedChangeListener(mCheckedListner); 

現在在您的點擊監聽器中檢查唯一的ID。

private RadioGroup.OnCheckedChangeListener mCheckedListner = new RadioGroup.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     JSONArray actionArray = new JSONArray(); 
     if(group.findViewById(checkedId)!=null) { 
      RadioButton rb = ((RadioButton) group.findViewById(checkedId)).getText(); 
//Your Code here 
       } 
      } 
     } 
    }; 
相關問題