如何根据意图字符串检查单选按钮
我正在尝试根据从先前活动发送的意图派生的值来检查单选按钮。屏幕加载后, 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在进行了一些故障排除后,我仍然找不到当前问题的答案。 getText() 方法未正确评估给定字符串。然而,解决这个问题的方法是直接将意图直接与具有与 radioButton.getText() 方法相同的文本的静态字符串变量进行比较,应该返回。
工作代码:
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: