gpt4 book ai didi

java - "Checking one radio button unchecks all the others"不工作

转载 作者:行者123 更新时间:2023-11-30 11:40:31 25 4
gpt4 key购买 nike

我正在开发 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);

我的问题是,当我点击一个未选中的单选按钮时,同一单选组中的另一个单选按钮仍处于选中状态。

这是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);
}
};

我做错了什么?

最佳答案

我找到了答案:**我需要在将所有单选按钮添加到单选组后设置选中的单选按钮。

这是我的工作代码:

    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.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.setOnClickListener(radioListener);
radGrp.addView(radioBtnNO);

我在单选组中添加了两个单选按钮,然后:

    if (confValue)
radioBtnYES.setChecked(true);
else
radioBtnNO.setChecked(true);

requirementLayout.addView(radGrp);

关于java - "Checking one radio button unchecks all the others"不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12671746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com