[英]Radio group not setting to radio buttons Android
我正在從數據庫中獲取數據。 基於該值,我正在動態創建單選按鈕。 當我嘗試將單選組添加到該單選按鈕時,單選組未設置。 我可以立即選擇許多單選按鈕。
attr_layout[i].addView(radioButton, lp);
attr_layout[i]` is the value for buttons which im getting. The values
are Small, Medium and Large.
這是RadioButton的完整代碼。
RadioGroup radioGroup = new RadioGroup(mMain);
LinearLayout[] attr_layout = new LinearLayout[optionsList.size()];
attr_layout[i] = new LinearLayout(mMain);
attr_layout[i].setOrientation(LinearLayout.HORIZONTAL);
int attr_size = attributes.size();
for (int k = 0; k < attr_size; k++)
{
String price = String.format(Locale.ENGLISH, AppConstants.DECIMAL_POINTS, Float.parseFloat(attributes.get(k).getAttr_price()));
String name_price = attributes.get(k).getAttr_name()
+" ("+ mMain.getString(R.string.currency_code)
+" "+ price +")";
if(!multiSelect.equals("1")) // This multiselect value 1 and 0 is coming from database.
//Based on these value, app will display checkbox or radio button
{
final RadioButton radioButton = new RadioButton(mMain);
radioButton.setText(name_price);
radioButton.setId(i + 6);
radioButton.setTextSize(12);
radioButton.setTag(attributes.get(k));
radioButton.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
radioButton.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
}
setTextFont(radioButton, "Museo_Slab.otf");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1f);
//lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
radioGroup.setLayoutParams(params);
attr_layout[i].addView(radioButton, lp);`
單選組僅允許一次檢查屬於它的一個按鈕(如果要檢查單選組的所有按鈕)。
將radioButtons添加到單選組中,然后添加到布局中。
使用代碼
radioGroup.addView(radioButton, lp);
attr_layout[i].addView(radioGroup)
代替
attr_layout[i].addView(radioButton, lp);`
radioGroup.addView(radioButton, lp);
而不是attr_layout[i].addView(radioButton, lp);
然后放入attr_layout[i].addView(radioGroup);
添加所有radioButton
之后,即在for
循環之外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.