2016-08-19 82 views
2

我是android編程新手。 我想在android應用程序內的滾動視圖內創建動態單選按鈕廣播組。 收音機組只有以水平方式顯示的限制。但我想要的是一個像單選按鈕模式的矩陣。在滾動視圖中添加動態RadioButtons

下面的圖片會給你我想要達到什麼的線索 第一張圖片就是我所能做到的。 第二個圖像是我想要實現的。

enter image description here

與此相關的另一個挑戰是,當一個人檢查了所有其他的單選按鈕必須得到選中。 (假設之後就被顯示在不同的無線電組。)

+1

提供您擁有或想要實現的代碼或屏幕截圖,以便我們開始瞭解您的需求。 – SWAT

+1

在此處顯示您的努力以獲得更好的幫助 – Vickyexpert

+0

請檢查此鏈接: https://i-msdn.sec.s-msft.com/en-us/windows/uwp/controls-and-patterns/images/radio-button -groups.png 我想要這樣的東西。 在此先感謝。 –

回答

1

下面的函數所需要的功能: -

/** 
*Returns a linear layout containing radio buttons in nX2 matrix fashion 
* where n is Math.Ceil(iRadioBtnDs.size/2) 
* 
* @param iRadioBtnInfoDs Data Structure containing RadioBtnInformation.. 
*      Contains id and label of the radio buttons. 
* @param iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs Currently checked radio button's index 
* 
* @return returns the linear layout containing the generated nX2 radio buttons. 
*/ 
public LinearLayout getLinearLayoutContainingRadioButtonInNX2MatrixFashion(Context iContext,ArrayList<DynamicRadioBtnDs> iRadioBtnInfoDs ,int iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs){ 


    final ArrayList<DynamicRadioBtnDs> loanTypeRadioBtns  =  iRadioBtnInfoDs; 

    float halfOfRadioCount         =  (float) iRadioBtnInfoDs.size()/2; 

    int noOfRadioGroupsNeeded        =  (int) Math.ceil(halfOfRadioCount); 

    LinearLayout radioGrpParentLl       =  new LinearLayout(iContext); 
    LinearLayout.LayoutParams paramsRadioGrpParentLl   =  new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    radioGrpParentLl.setLayoutParams(paramsRadioGrpParentLl); 
    radioGrpParentLl.setOrientation(LinearLayout.VERTICAL); 

    for (int i = 0; i < noOfRadioGroupsNeeded; i++) { 

     final int indexOfCurrentRadioGrpInParentLl = i; 

     //Creating i th radio group... 

     final RadioGroup radioGroupCurrentInteration     =  new RadioGroup(iContext); 
     RadioGroup.LayoutParams paramsRadio   =  new RadioGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     radioGroupCurrentInteration.setOrientation(RadioGroup.HORIZONTAL); 
     radioGroupCurrentInteration.setLayoutParams(paramsRadio); 

     //Creating 2*i th radio button... 
     //This will always be in the left... 

     RadioButton leftRadioButtonInCurrentRadioGroup = new RadioButton(iContext); 
     RadioGroup.LayoutParams paramsLeftRadioBtnInCurrentRadioGroup = new RadioGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT); 

     String leftRadionBtnLabel = loanTypeRadioBtns.get(2 * i).getLabel(); 
     leftRadioButtonInCurrentRadioGroup.setText(leftRadionBtnLabel); 
     leftRadioButtonInCurrentRadioGroup.setLayoutParams(paramsLeftRadioBtnInCurrentRadioGroup); 

     radioGroupCurrentInteration.addView(leftRadioButtonInCurrentRadioGroup); 
     paramsLeftRadioBtnInCurrentRadioGroup.weight = 1f; 
     leftRadioButtonInCurrentRadioGroup.setTypeface(Typeface.DEFAULT_BOLD); 

     class CustomCheckedChangeListener implements CompoundButton.OnCheckedChangeListener { 

      private int indexOfCurrentlyCheckedRbInRadioBtnInfoDs; 

      CustomCheckedChangeListener(int iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs) { 
       indexOfCurrentlyCheckedRbInRadioBtnInfoDs  = iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs; 
      } 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (isChecked) { 
        indexOfCurrentlyCheckedRbInRadioBtnInfoDs = indexOfCurrentRadioGrpInParentLl * 2; 
       } 
      } 
     } 

     CustomCheckedChangeListener checkChangedListenerForLeftRb = new CustomCheckedChangeListener(iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs); 

     leftRadioButtonInCurrentRadioGroup.setOnCheckedChangeListener(checkChangedListenerForLeftRb); 



     //Right radio button may or may not be required.. 
     //(i+1) represents the level starting from 1.... 
     //For example i+1 is 5 means we are now in the 5th iteration 
     //ie 5th radio group... 

     //If we need right radio button, 
     //The size will be equal to no of radio groups*2 

     if((i+1)*2 <=loanTypeRadioBtns.size()) { 
      //The right radio group is needed 

      RadioButton rightRadioButtonInCurrentRadioGroup      = new RadioButton(iContext); 
      RadioGroup.LayoutParams paramsRightRadioBtnInCurrentRadioGroup  = new RadioGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT); 
      rightRadioButtonInCurrentRadioGroup.setLayoutParams(paramsLeftRadioBtnInCurrentRadioGroup); 
      paramsRightRadioBtnInCurrentRadioGroup.weight      = 1f; 
      rightRadioButtonInCurrentRadioGroup.setTypeface(Typeface.DEFAULT_BOLD); 
      String rightRadioBtnLabel           = loanTypeRadioBtns.get((2 * i) + 1).getLabel(); 
      rightRadioButtonInCurrentRadioGroup.setText(rightRadioBtnLabel); 
      radioGroupCurrentInteration.addView(rightRadioButtonInCurrentRadioGroup); 


      class CustomCheckedChangeListenerForRightRb implements CompoundButton.OnCheckedChangeListener { 

       private int indexOfCurrentlyCheckedRbInRadioBtnInfoDs; 

       CustomCheckedChangeListenerForRightRb(int iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs) { 
        indexOfCurrentlyCheckedRbInRadioBtnInfoDs  = iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs; 
       } 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        if (isChecked) { 
         indexOfCurrentlyCheckedRbInRadioBtnInfoDs = (indexOfCurrentRadioGrpInParentLl * 2)+1; 
        } 
       } 
      } 

      CustomCheckedChangeListenerForRightRb checkChangedListenerForRightRb = new CustomCheckedChangeListenerForRightRb(iIndexOfCurrentlyCheckedRbInRadioBtnInfoDs); 

      rightRadioButtonInCurrentRadioGroup.setOnCheckedChangeListener(checkChangedListenerForRightRb); 


     } 

     radioGroupCurrentInteration.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 

       RadioButton checkChangedRadioBtn = (RadioButton) group.findViewById(checkedId); 
       if (checkChangedRadioBtn.isChecked()) { 
        //Some radio button in currentradioGrpParentLl 
        //Radio group has been checked.... 
        LinearLayout loanEmiParentLl = (LinearLayout) group.getParent(); 
        for (int i = 0; i < loanEmiParentLl.getChildCount(); i++) { 
         RadioGroup childRadioGroupAtIndexI = (RadioGroup) loanEmiParentLl.getChildAt(i); 
         if (!childRadioGroupAtIndexI.equals(group)) { 
          //Means the current radio group is a sister radio group.... 
          //So clear checks... 
          childRadioGroupAtIndexI.setOnCheckedChangeListener(null); 
          childRadioGroupAtIndexI.clearCheck(); 
          childRadioGroupAtIndexI.setOnCheckedChangeListener(this); 

         } else { 
          //Current radio group... 
          //So do nothing... 
         } 
        } 
       } 
      } 
     }); 

     radioGrpParentLl.addView(radioGroupCurrentInteration); 
    } 

    return radioGrpParentLl; 

} 







public class DynamicRadioBtnDs { 

    DynamicRadioBtnDs(String iId,String iLabel){ 
     id  = iId; 
     label = iLabel; 
    } 

    private String id; 
    private String label; 


    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getLabel() { 
     return label; 
    } 

    public void setLabel(String label) { 
     this.label = label; 
    } 
} 

用戶可以改變DynamicRadioBtnDs爲String如果他們不打算處理單選按鈕的id。

+0

謝謝,這對我有用。 –

0

您可以動態地添加任何意見滾動型爲如:

layoutInflater = LayoutInflater.from(this); 
layoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1); 
layoutParam.setMargins(10, 0, 10, 10); 

for (int i = 0; i < 10; i++) {    
    View view = layoutInflater.inflate(R.layout.deals_style, null); 
    ((TextView) deals.findViewById(R.id.tv_product_name)) 
     .setText("my text") 

     layoutNewDeals.addView(deals, layoutParam); 
} 

更換的TextView單選按鈕

你必須創建一個自定義佈局文件或運行時創建一個單選按鈕

1

在你的XML佈局

 <LinearLayout 
      android:id="@+id/linear1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="10dp" 
      android:orientation="vertical"> 

      <RadioGroup 
       android:id="@+id/radiogroup" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:orientation="vertical" /> 
     </LinearLayout> 

添加這一點,並在你的活動,你想有添加此您的單選按鈕

RadioGroup rg = (RadioGroup) findViewById(R.id.radiogroup); 
RadioButton[] rb = new RadioButton[items.size()]; 
for (int i = 0; i < items.size(); i++) { 
    rb[i] = new RadioButton(this); 
    rg.addView(rb[i]); 
    rb[i].setText(items.get(i).getName()); 
} 
1

使用此

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RadioGroup android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/radioGroup"> 

    </RadioGroup> 
</LinearLayout> 

您可以動態地添加單選按鈕,看起來像下面

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup); 
    for (int i=0;i<10,i++) { 
     RadioButton radioButton = new RadioButton(getBaseContext()); 
     radioGroup.addView(radioButton); 
    } 
+0

這不會以矩陣方式顯示單選按鈕,請參閱更新後的問題。 –