如何从字符串数组中提取名称属性?

发布于 2024-12-01 23:51:42 字数 628 浏览 5 评论 0原文

您好,我构建了一个测验应用程序。 我有以下 (values/)question.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="question">
<item name="correct">A</item>
<item name="wrong">B</item>
<item name="wrong">C</item>
<item name="wrong">D</item>
</string-array>
</resources>

我想要一个有四个可能答案的问题,但是当我在 Java 中检索答案时..我不知道哪个答案是正确的。因此,我决定在项目标签中使用名称属性来传递“正确”或“错误”答案的值。 有没有办法获取名称和标签值?

因为当我使用 String[] test = res.getStringArray(R.array.question);我只能获取数组中每个项目的值。

或者因为这是我第一次使用 Android。还有其他合适的方法来做到这一点吗?

谢谢

Hi I build a quiz application.
I have the following (values/)question.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="question">
<item name="correct">A</item>
<item name="wrong">B</item>
<item name="wrong">C</item>
<item name="wrong">D</item>
</string-array>
</resources>

I would like to have a question with four possible answers but when i retrieve my answers in Java.. I don't know which answer is correct. So I decided to use name attribute in the item tags to pass a value of 'correct' or 'wrong' answer.
Is there anyway to get the name along with the tag value?

because when i use String[] test = res.getStringArray(R.array.question); I can only get the value of each item in my array.

or because this is my 1st time in Android. is there other suitable approach to do this?

thanks

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

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

发布评论

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

评论(3

〆凄凉。 2024-12-08 23:51:42

需要使用Handler来解析xml。
要获取属性值,代码是:

attributes.getValue("name")

尝试这些链接以供参考:
首先
第二个

You need to use Handler to parse the xml.
to get attribute value, code is :

attributes.getValue("name")

Try these links for reference:
first and
second

为你拒绝所有暧昧 2024-12-08 23:51:42

我做了很多测验,一种很好的方法是将正确的选项始终放在问题之后的顶部。您可以使用随机函数在活动中显示时随机排列选项。

I have made many quizzes, and one nice way is to put correct option always on top, right after question. You can use random function to shuffle options while displaying in an activity.

离笑几人歌 2024-12-08 23:51:42

在活动中:
String[] QuestionArray = getResources().getStringArray(R.id.question);
那么你有

  • QuestionArray[0] ->一个
  • 问题Array[1] -> B
  • 问题数组[2] -> C
  • 问题数组[3] -> D

in Activity:
String[] questionArray = getResources().getStringArray(R.id.question);
then you have

  • questionArray[0] -> A
  • questionArray[1] -> B
  • questionArray[2] -> C
  • questionArray[3] -> D
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文