我正在使用单选按钮和复选框按钮。值0表示无线电,1表示复选框。基于这些值,我想在屏幕上显示它。可能有收音机和复选框。
现在,值im从数据库中获取为0和1,它只是为所有属性设置单选按钮或复选框。它应显示值1的复选框和值为0的单选按钮。
以下是代码:
if(multiSelect != null)
{
RadioButton radioButton = new RadioButton(mMain);
radioButton.setText(name_price);
radioButton.setId(i + 6);
radioButton.setTextSize(12);
radioButton.setTag(attributes.get(num));
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);
radioButton.setLayoutParams(lp);
attr_layout[j].addView(radioButton);
num++;
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
try
{
ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag();
if (isChecked)
{
float total_price = current_price + attr_price;
item.setItemPrice(total_price + "");
item_price_text.setText(priceStr);
selectedAttributes.add(itemAttributes);
}
// If the attributes are not checked
} catch (Exception ex)
{
GSLogger.e(ex);
}
}
});
}
else // if the multiSelect is 1
{
CheckBox checkBox = new CheckBox(mMain);
checkBox.setText(name_price);
checkBox.setId(i + 6);
checkBox.setTextSize(12);
checkBox.setTag(attributes.get(num));
checkBox.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
checkBox.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
}
setTextFont(checkBox, "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);
checkBox.setLayoutParams(lp);
attr_layout[j].addView(checkBox);
num++;
// Reads the value depending on attribute User Selects
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
try
{
ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag();
if (isChecked)
{
float total_price = current_price + attr_price;
item.setItemPrice(total_price + "");
item_price_text.setText(priceStr);
selectedAttributes.add(itemAttributes);
}
} catch (Exception ex)
{
GSLogger.e(ex);
}
}
});
}
}
}
}
根据定义的项目组说明它是0或1,它应该在屏幕上显示。如果该组同时具有两个选项,则屏幕应显示无线电为0值,复选框为1值。
答案 0 :(得分:1)
您的if条件只允许添加其中任何一个。你应该检查值是否为0然后添加单选按钮,如果值为1则应该添加复选框。如果是块,你已经实现了if else块而不是2。
if(value == 0)
{
RadioButton radioButton = new RadioButton(mMain);
radioButton.setText(name_price);
radioButton.setId(i + 6);
radioButton.setTextSize(12);
radioButton.setTag(attributes.get(num));
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);
radioButton.setLayoutParams(lp);
attr_layout[j].addView(radioButton);
num++;
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
try
{
ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag();
if (isChecked)
{
float total_price = current_price + attr_price;
item.setItemPrice(total_price + "");
item_price_text.setText(priceStr);
selectedAttributes.add(itemAttributes);
}
// If the attributes are not checked
} catch (Exception ex)
{
GSLogger.e(ex);
}
}
});
}
if(value==1)
{
CheckBox checkBox = new CheckBox(mMain);
checkBox.setText(name_price);
checkBox.setId(i + 6);
checkBox.setTextSize(12);
checkBox.setTag(attributes.get(num));
checkBox.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
checkBox.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
}
setTextFont(checkBox, "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);
checkBox.setLayoutParams(lp);
attr_layout[j].addView(checkBox);
num++;
// Reads the value depending on attribute User Selects
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
try
{
ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag();
if (isChecked)
{
float total_price = current_price + attr_price;
item.setItemPrice(total_price + "");
item_price_text.setText(priceStr);
selectedAttributes.add(itemAttributes);
}
} catch (Exception ex)
{
GSLogger.e(ex);
}
}
});
}
答案 1 :(得分:0)
如果从db获得0,则更改条件,然后设置单选按钮,否则设置复选框
if(value == 0)
{
RadioButton radioButton = new RadioButton(mMain);
radioButton.setText(name_price);
radioButton.setId(i + 6);
radioButton.setTextSize(12);
radioButton.setTag(attributes.get(num));
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);
radioButton.setLayoutParams(lp);
attr_layout[j].addView(radioButton);
num++;
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
try
{
ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag();
if (isChecked)
{
float total_price = current_price + attr_price;
item.setItemPrice(total_price + "");
item_price_text.setText(priceStr);
selectedAttributes.add(itemAttributes);
}
// If the attributes are not checked
} catch (Exception ex)
{
GSLogger.e(ex);
}
}
});
}
else
{
CheckBox checkBox = new CheckBox(mMain);
checkBox.setText(name_price);
checkBox.setId(i + 6);
checkBox.setTextSize(12);
checkBox.setTag(attributes.get(num));
checkBox.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
checkBox.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
}
setTextFont(checkBox, "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);
checkBox.setLayoutParams(lp);
attr_layout[j].addView(checkBox);
num++;
// Reads the value depending on attribute User Selects
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
try
{
ItemAttributes itemAttributes = (ItemAttributes) buttonView.getTag();
if (isChecked)
{
float total_price = current_price + attr_price;
item.setItemPrice(total_price + "");
item_price_text.setText(priceStr);
selectedAttributes.add(itemAttributes);
}
} catch (Exception ex)
{
GSLogger.e(ex);
}
}
});
}
}
}
}