Android 在滚动视图中添加动态单选按钮

Android 在滚动视图中添加动态单选按钮,android,Android,我是android编程新手。 我想在android应用程序的滚动视图中创建动态单选按钮单选组。 广播组的限制是只能以水平方式显示。但我想要的是一个矩阵式的单选按钮 下图将为您提供我想要实现的目标的线索 第一个图像是我能做的。 第二个图像是我想要实现的 与此相关的另一个挑战是,当一个单选按钮被选中时,所有其他单选按钮都必须被取消选中。(假设它们显示在不同的广播组中。)您可以将任何视图动态添加到滚动视图中,例如: layoutInflater = LayoutInflater.from(this)

我是android编程新手。 我想在android应用程序的滚动视图中创建动态单选按钮单选组。 广播组的限制是只能以水平方式显示。但我想要的是一个矩阵式的单选按钮

下图将为您提供我想要实现的目标的线索 第一个图像是我能做的。 第二个图像是我想要实现的


与此相关的另一个挑战是,当一个单选按钮被选中时,所有其他单选按钮都必须被取消选中。(假设它们显示在不同的广播组中。)

您可以将任何视图动态添加到滚动视图中,例如:

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);
}
layoutInflater=layoutInflater.from(this);
layoutParam=新建LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,LinearLayout.LayoutParams.WRAP_内容,1);
layoutParam.setMargins(10,0,10,10);
对于(int i=0;i<10;i++){
视图=LayoutFlater.inflate(R.layout.U样式,空);
((TextView)deals.findviewbyd(R.id.tv\u product\u name))
.setText(“我的文本”)
layoutNewDeals.addView(deals,layoutParam);
}
文本视图替换为单选按钮


您必须在运行时创建自定义布局文件或创建单选按钮

将其添加到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());
}
<?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 rg=(RadioGroup)findviewbyd(R.id.RadioGroup);
RadioButton[]rb=新的RadioButton[items.size()];
对于(int i=0;i
使用此

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());
}
<?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);
    }
RadioGroup-RadioGroup=(RadioGroup)findViewById(R.id.RadioGroup);

对于(int i=0;i而言,以下函数执行所需的功能:-

/**
 *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;
    }
}
/**
*以nX2矩阵方式返回包含单选按钮的线性布局
*其中n是Math.Ceil(iRadioBtnDs.size/2)
*
*@param iRadioBtnInfoDs包含放射性信息的数据结构。。
*包含单选按钮的id和标签。
*@param IINDEXOFCURRENTLYCHECKEDRBINRADIOBTINFODS当前已检查单选按钮的索引
*
*@return返回包含生成的nX2单选按钮的线性布局。
*/
public LinearLayout GetLinearLayout包含InGradioButtonNx2MatrixFashion(上下文iContext、数组列表IradioBTInFods、int IIIndexOfcCurrentlyCheckedrBinRadioBTInFods){
最终ArrayList LoantPeradiobtns=iRadioBtnInfoDs;
float halfOfRadioCount=(float)iRadioBtnInfoDs.size()/2;
int noofradiogroupsNeed=(int)Math.ceil(halfOfRadioCount);
LinearLayout RadiorParentll=新的LinearLayout(iContext);
LinearLayout.LayoutParams paramsradiogrparentll=新建LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL\u父级,LinearLayout.LayoutParams.WRAP\u内容);
RadiorParentll.setLayoutParams(ParamsRadiogParentll);
放射线管设置方向(线性布局、垂直);
for(int i=0;i