单选组中的单选按钮单击后不显示选中状态

发布于 2024-12-10 16:56:50 字数 1603 浏览 0 评论 0原文

我在使用单选组中的单选按钮时遇到问题。

我有一个测验应用程序,我问 5 个问题。

当在我的 Android 模拟器中运行应用程序时,所有问题都没有问题,但只有第三个问题。当我用鼠标单击单选按钮时,它似乎切换到选中状态,但随后立即取消选中。有谁见过这种行为吗?!

我设置了这个单选组并在单选组中动态添加 4 个单选按钮,然后使用 OnCheckedChangeListener() 事件来捕获更改。

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                for(int i=0; i<=3;i++)
                {
                    RadioButton btn = (RadioButton) radioGroup.getChildAt(i);

                    if (btn.isPressed() && questNo < 6)
                    {

                        if (corrAns[questNo-1].equals(btn.getText()) && flag==true)
                        {
                            Log.e(LOG_TAG,"onCheckedChanged: correct answer = btn Text");
                            score++;
                            flag = false;
                            checked = true; 
                        }
                        else if(checked==true)
                        {
                            Log.e(LOG_TAG,"onCheckedChanged: correct answer != btn Text");
                            score--;
                            flag = true;
                            checked=false;
                        }
                    }
                }

                Log.e(LOG_TAG, "Score:"+ Integer.toString(score));
            }
        });

我注意到它在不同的问题上随机发生,并且仅在选择的第一个单选按钮上发生,但如果您之后选择另一个单选按钮,则功能将恢复正常。有什么想法吗?

I am having a problem with a radio button that is in a radio group.

I have an app that is a quiz application and I ask 5 questions.

When run the application in my Android emulator all the questions have no problem but only on the 3rd question. When I click with my mouse on the radio button it seems toggle to a checked state but then it unchecks right away. Has anyone seen this kind of behavior?!

I setup this Radio group and dynamically add 4 radio buttons in a radio group and then use an OnCheckedChangeListener() event to capture the change.

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                for(int i=0; i<=3;i++)
                {
                    RadioButton btn = (RadioButton) radioGroup.getChildAt(i);

                    if (btn.isPressed() && questNo < 6)
                    {

                        if (corrAns[questNo-1].equals(btn.getText()) && flag==true)
                        {
                            Log.e(LOG_TAG,"onCheckedChanged: correct answer = btn Text");
                            score++;
                            flag = false;
                            checked = true; 
                        }
                        else if(checked==true)
                        {
                            Log.e(LOG_TAG,"onCheckedChanged: correct answer != btn Text");
                            score--;
                            flag = true;
                            checked=false;
                        }
                    }
                }

                Log.e(LOG_TAG, "Score:"+ Integer.toString(score));
            }
        });

I have noticed that it happens randomly on different questions and only on first radiobutton that is selected but if you select another one after then the functionality returns to normal. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混浊又暗下来 2024-12-17 16:56:50

我有同样的错误。我还使用动态单选组和按钮。这对我有用:

private OnCheckedChangeListener rblLikert_Listener = new OnCheckedChangeListener() {
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int selectedIndex = group.getCheckedRadioButtonId();

        if(selectedIndex != -1)
        {
            m_likertValue = radioButtonValue;
            int buttonId = group.getCheckedRadioButtonId();
            Logger.i("button id: " + String.valueOf(buttonId));
            RadioButton selectedButton = (RadioButton)findViewById(buttonId);
            selectedButton.toggle();
            Logger.i(" is checked: " + String.valueOf(selectedButton.isChecked()));
        }
    }
};

I had the same bug. I'm also using dynamic radioGroups and buttons. This is working for me:

private OnCheckedChangeListener rblLikert_Listener = new OnCheckedChangeListener() {
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int selectedIndex = group.getCheckedRadioButtonId();

        if(selectedIndex != -1)
        {
            m_likertValue = radioButtonValue;
            int buttonId = group.getCheckedRadioButtonId();
            Logger.i("button id: " + String.valueOf(buttonId));
            RadioButton selectedButton = (RadioButton)findViewById(buttonId);
            selectedButton.toggle();
            Logger.i(" is checked: " + String.valueOf(selectedButton.isChecked()));
        }
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文