【问题标题】:android - controlling state of RadioButtonandroid - 控制 RadioButton 的状态
【发布时间】:2011-03-15 06:12:38
【问题描述】:

我在我制作的 d android 应用程序中遇到了 1 个问题。

-> 我在 1 个活动屏幕上有 3 个 RadioButton

-> 按下按钮后(在同一活动上),与所有按钮关联的文本应更改,并且所有 RadioButton 必须未选中且应可点击

文本正在正确更改,并且 RadioButton 被取消选中,但面临的问题是:

  1. 如果假设 RadioButton 1 被选中,然后在按下 Button 后,我无法点击同一个 RadioButton,直到我点击其他 RadioButton。

为什么会这样??

【问题讨论】:

  • 你能发一些代码给我们看看吗?

标签: android android-activity radio-button


【解决方案1】:

第 1 部分

RadioButton option1  = (RadioButton) findViewById(R.id.RadioButton01);
option1.setText(temp.substring(answerCount[0]+1, answerCount[1]));

if(option1.isChecked())
{
    score+=1;
}

第二部分

option1.setChecked(false);
option1.setClickable(true);
option1.setText(temp.substring(answerCount[aCount]+1, answerCount[aCount+1]));

第 1 部分执行 Ist,然后第 2 部分是我的方法 1 的一部分.. 够了吗?

【讨论】:

  • 为什么不直接编辑原始问题以包含此代码?
  • 不,我发现了错误。在我的 .java 中,我有所有单选按钮的实例,但没有单选按钮的实例。一旦我创建了 RadioGroup 实例,我就调用 clearCheck() 来解决这个问题。无论如何,谢谢你的帮助
【解决方案2】:

解决方案是为未来的读者准备的

在 .java 文件中创建 RadioGroup 实例并调用 instance.clearCheck()

【讨论】:

    【解决方案3】:

    不要直接使用 RadioButton.setChecked 方法。 使用 RadioGroup.check 方法以编程方式检查 RadioButton

    rg = (RadioGroup) findViewById(R.id.myFavouriteGroup);
    int selectedItem = 0; // the fist item in rgGroup must be selected
    .... do something ....
    if (selectedItem >= 0)
    {
       rg.check(rg.getChildAt(selectedItem).getId());
    }
    ....
    

    此用户可以将所选项目更改为另一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 2019-11-25
      相关资源
      最近更新 更多