我正在开发一个 Android 3.1 平板电脑应用程序,我需要以编程方式创建一些单选按钮:
RadioGroup radGrp = new RadioGroup(mActivity);
params.weight = 0.2f;
radGrp.setLayoutParams(params);
radGrp.setOrientation(RadioGroup.HORIZONTAL);
String tag = Long.toString(req.getRequirementId()) + "_" +
getString(R.string.yes);
RadioButton radioBtnYES = new RadioButton(mActivity);
radioBtnYES.setText(getString(R.string.yes));
radioBtnYES.setTag(tag);
radioBtnYES.setChecked(confValue);
radioBtnYES.setOnClickListener(radioListener);
radGrp.addView(radioBtnYES);
tag = Long.toString(req.getRequirementId()) + "_" +
getString(R.string.no);
RadioButton radioBtnNO = new RadioButton(mActivity);
radioBtnNO.setText(getString(R.string.no));
radioBtnNO.setTag(tag);
radioBtnNO.setChecked(!confValue);
radioBtnNO.setOnClickListener(radioListener);
radGrp.addView(radioBtnNO);
radGrp.setOnCheckedChangeListener(checkedChangedListener);
requirementLayout.addView(radGrp);
我的问题是,当我点击一个未选中的单选按钮时,同一个 Radio Group 上的另一个按钮仍然处于选中状态。
这是radioListener
代码:
private RadioButton.OnClickListener radioListener =
new RadioButton.OnClickListener()
{
@Override
public void onClick(View v)
{
String tag = v.getTag().toString();
String[] parts = tag.split("_");
if ((parts != null) && (parts.length == 2))
{
boolean value = (parts[1].equals(getString(R.string.yes)));
Long id = Long.valueOf(parts[0]);
requirementsState.put(id, value);
}
}
};
这是checkedChangedListener
代码:
private RadioGroup.OnCheckedChangeListener checkedChangedListener =
new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton rb = (RadioButton)
mActivity.findViewById(checkedId);
group.setOnCheckedChangeListener(null);
group.clearCheck();
rb.setChecked(true);
group.setOnCheckedChangeListener(checkedChangedListener);
}
};
我究竟做错了什么?