android radiogroup 添加,如何在android中动态添加textview到radiogroup

大家好, 我们想动态地在radiogroup旁边添加textview。基于服务响应,我们需要在不使用xml的情况下将标签添加到radiogroup。 在某些情况下基于最长无线电如何在android中动态添加textview到radiogroup

[

8b25ca4f5bca64774e3d622ebf00d788.png]

这里下面的代码

final RadioGroup rg = new RadioGroup(this); //create the RadioGroup

rg.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL

rg.setGravity(Gravity.CENTER);

final RadioButton[] rb = new RadioButton[items_list.size()];

final CheckBox[] cb = new CheckBox[items_list.size()];

for (int i = 0; i < items_list.size(); i++) {

LinearLayout ll_orientaion = new LinearLayout(this);

ll_orientaion.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f));

ll_orientaion.setOrientation(LinearLayout.HORIZONTAL);

ll_orientaion.setBackgroundColor(getResources().getColor(R.color.white));

ll_orientaion.setWeightSum(2);

ll_orientaion.setGravity(Gravity.CENTER);

if (max == 1) {

rb[i] = new RadioButton(this);

rb[i].setText(pd.getName());

rb[i].setGravity(Gravity.CENTER);

ll_orientaion.addView(rb[i]);

if (pd.getSelected_status().equals("true")) {

rb[i].setChecked(true);

}

hashMap_rdlist.put(rb[i].getText().toString(), rdd);

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

ProductOptions rd_getlist = new ProductOptions();

int checkedRadioButtonId = rg.getCheckedRadioButtonId();

RadioButton radioButton = (RadioButton) findViewById(checkedRadioButtonId);

// Toast.makeText(ProductOptionsActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();

String rd_name = (String) radioButton.getText();

});

} else {

cb[i] = new CheckBox(this);

cb[i].setButtonTintList(ColorStateList.valueOf(getColor(R.color.colorPrimary)));

cb[i].setText(pd.getName());

cb[i].setGravity(Gravity.CENTER);

ll_orientaion.addView(cb[i]);

}

final int finalI = i;

cb[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

ProductOptions rd_getlist = new ProductOptions();

String check_name = (String) cb[finalI].getText();

if (isChecked) {

check_count++;

} else {

check_count--;

}

}

});

}

// Log.d("checklist",""+productoptions_skuId);

TextView tv = new TextView(this);

LinearLayout.LayoutParams tv1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);

tv.setLayoutParams(tv1);

tv1.weight = 2;

tv1.gravity = Gravity.CENTER;

tv.setText(pd.getSalePrice());

tv.setTextSize(14);

tv.setGravity(Gravity.END);

if (pd.getSalePrice().equals("0.0")) {

tv.setVisibility(View.INVISIBLE);

}

ll_orientaion.addView(tv);

rg.addView(ll_orientaion);

}

主要的问题,当我们添加的LinearLayout到radiGroup查看越来越完美,但单选按钮动作即不工作,

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {}

请鬼谷de us 高级谢谢!

+0

'RadioGroup'只能用于它自己的子节点,或者是RadioButton的子类。看来你正在将'LinearLayout'添加为'RadioGroup'子元素。 –

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值