【问题标题】:Radio button is not displaying right value单选按钮未显示正确的值
【发布时间】:2013-01-08 01:15:48
【问题描述】:

在我的编码中,m 有三个按钮。我得到选定的按钮是 id 并将其用于比较。但在单击按钮之前,它首先会自动显示默认选定按钮的 id。

rdgp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                counter++;
                if (counter > 5 || counter >= anarray.size()) {
                    Toast.makeText(testActivity.this, "" + score, 2000).show();
                    Intent intent = new Intent(testActivity.this, Badge.class);
                    intent.putExtra("Score", score);
                    startActivity(intent);
                    return;
                }

                rb1.setText(anarray.get(counter));
                rb2.setText(an1array.get(counter));
                rb3.setText(an2array.get(counter));
                rb4.setText(an3array.get(counter));
                rb5.setText(an4array.get(counter));
                int ans = 0;
                if (checkedId == R.id.radio0) {
                    ans = 1;
                } else if (checkedId == R.id.radio1) {
                    ans = 2;
                } else if (checkedId == R.id.radio2) {
                    ans = 3;
                } else if (checkedId == R.id.radio3) {
                    ans = 4;
                }

                Toast.makeText(testActivity.this, "" + ans, 2000).show();
                if (ans == anarray.get(counter))
                    ;
                {
                    score = score + 1;
                }
            }
        });

【问题讨论】:

    标签: android button radio-button


    【解决方案1】:

    要获取选中的radiobutton 的ID,请使用group.getCheckedRadioButtonId()。返回此组中选定单选按钮的标识符。空选时,返回值为-1。

    【讨论】:

      【解决方案2】:

      在您的 XML 中为所有单选按钮添加以下行。

       android:checked="false"
      

      【讨论】:

      • 它适用于第一组数据。当 onclick 移动到下一组数据时,显示第一组数据中的单击按钮并选择该值进行比较。
      【解决方案3】:

      如果您从单选按钮获取默认值,请检查您使用的布局文件天气 android:checked = "true"。如果这是状态,则将其更改为 false。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 2013-08-04
        • 2014-03-01
        • 1970-01-01
        相关资源
        最近更新 更多