如何根据意图字符串检查单选按钮

发布于 2025-01-14 18:14:37 字数 2667 浏览 3 评论 0原文

我正在尝试根据从先前活动发送的意图派生的值来检查单选按钮。屏幕加载后, radioButton.getText().toString() 应与特定值进行比较,并检查该特定单选按钮是否评估为 true,但我没有得到预期结果。

我查看了堆栈溢出和各种文档/教程网站,但我没有遇到其他人遇到这个特定问题。谁能指出我解决这个问题的正确方向? 这是相关的活动代码:

courseStatus = getIntent().getStringExtra("course_status");
editCourseStatusG = findViewById(R.id.radioGroup1);
editStatusComplete = findViewById(R.id.editStatusCompleted);
editStatusDropped = findViewById(R.id.editStatusDropped);
editStatusOngoing = findViewById(R.id.editStatusOngoing);
editStatusPlanned = findViewById(R.id.editStatusPlanned);

    
if(courseStatus.equals(editStatusComplete.getText().toString())) {
        editStatusComplete.setChecked(true);
} else if (courseStatus.equals(editStatusDropped.getText().toString())) {
        editStatusDropped.setChecked(true);
} else if ((courseStatus.equals(editStatusOngoing.getText().toString()))) {
        editStatusOngoing.setChecked(true);
} else if ((courseStatus.equals(editStatusPlanned.getText().toString()))) {
        editStatusPlanned.setChecked(true);
}

XML 代码:

<RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <RadioButton
                    android:id="@+id/editStatusOngoing"
                    android:layout_width="match_parent"
                    android:text="Ongoing"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:layout_weight="1"/>
                <RadioButton
                    android:id="@+id/editStatusCompleted"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Completed"
                    android:ems="10"
                    android:layout_weight="1"/>
                <RadioButton
                    android:id="@+id/editStatusDropped"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:text="Dropped"
                    android:layout_weight="1"/>
                <RadioButton
                    android:id="@+id/editStatusPlanned"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:text="Planned"
                    android:layout_weight="1"/>

            </RadioGroup>

I'm trying to check a radio button based upon an value derived from an intent sent from the previous activity. Upon screen load, the radioButton.getText().toString() should compare against a specific value and check that particular radio button if it evaluates to true but I am not getting the expected results.

I've looked on stack overflow and various documentation/tutorial sites but I haven't came across anyone else having this particular issue. Can anyone point me in the right direction of solving this issue?
Here's the relevant activity code :

courseStatus = getIntent().getStringExtra("course_status");
editCourseStatusG = findViewById(R.id.radioGroup1);
editStatusComplete = findViewById(R.id.editStatusCompleted);
editStatusDropped = findViewById(R.id.editStatusDropped);
editStatusOngoing = findViewById(R.id.editStatusOngoing);
editStatusPlanned = findViewById(R.id.editStatusPlanned);

    
if(courseStatus.equals(editStatusComplete.getText().toString())) {
        editStatusComplete.setChecked(true);
} else if (courseStatus.equals(editStatusDropped.getText().toString())) {
        editStatusDropped.setChecked(true);
} else if ((courseStatus.equals(editStatusOngoing.getText().toString()))) {
        editStatusOngoing.setChecked(true);
} else if ((courseStatus.equals(editStatusPlanned.getText().toString()))) {
        editStatusPlanned.setChecked(true);
}

XML code:

<RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <RadioButton
                    android:id="@+id/editStatusOngoing"
                    android:layout_width="match_parent"
                    android:text="Ongoing"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:layout_weight="1"/>
                <RadioButton
                    android:id="@+id/editStatusCompleted"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Completed"
                    android:ems="10"
                    android:layout_weight="1"/>
                <RadioButton
                    android:id="@+id/editStatusDropped"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:text="Dropped"
                    android:layout_weight="1"/>
                <RadioButton
                    android:id="@+id/editStatusPlanned"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:text="Planned"
                    android:layout_weight="1"/>

            </RadioGroup>

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

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

发布评论

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

评论(1

不必你懂 2025-01-21 18:14:37

在进行了一些故障排除后,我仍然找不到当前问题的答案。 getText() 方法未正确评估给定字符串。然而,解决这个问题的方法是直接将意图直接与具有与 radioButton.getText() 方法相同的文本的静态字符串变量进行比较,应该返回。

工作代码:

       editCourseStatusG = findViewById(R.id.radioGroup1);
       editStatusComplete = findViewById(R.id.editStatusCompleted);
       editStatusDropped = findViewById(R.id.editStatusDropped);
       editStatusOngoing = findViewById(R.id.editStatusOngoing);
       editStatusPlanned = findViewById(R.id.editStatusPlanned);

       try {
           switch (courseStatus) {
               case "Completed":
                   editStatusComplete.setChecked(true);
                   break;
               case "Dropped":
                   editStatusDropped.setChecked(true);
                   break;
               case "Ongoing":
                   editStatusOngoing.setChecked(true);
                   break;
               case "Planned":
                   editStatusPlanned.setChecked(true);
                   break;
               default:
                   editStatusPlanned.setChecked(true);
           }
       } catch(NullPointerException e) {
           editStatusPlanned.setChecked(true);
       }

After doing some troubleshooting, I still cannot find the answer to the issue at hand. The getText() method is not evaluating correctly to the given string. However, a work around it to compare directly the intent directly to a static string variable with the same text as the radioButton.getText() method should return.

Working code:

       editCourseStatusG = findViewById(R.id.radioGroup1);
       editStatusComplete = findViewById(R.id.editStatusCompleted);
       editStatusDropped = findViewById(R.id.editStatusDropped);
       editStatusOngoing = findViewById(R.id.editStatusOngoing);
       editStatusPlanned = findViewById(R.id.editStatusPlanned);

       try {
           switch (courseStatus) {
               case "Completed":
                   editStatusComplete.setChecked(true);
                   break;
               case "Dropped":
                   editStatusDropped.setChecked(true);
                   break;
               case "Ongoing":
                   editStatusOngoing.setChecked(true);
                   break;
               case "Planned":
                   editStatusPlanned.setChecked(true);
                   break;
               default:
                   editStatusPlanned.setChecked(true);
           }
       } catch(NullPointerException e) {
           editStatusPlanned.setChecked(true);
       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文