Android 编程时如何正确使用哈希映射?

发布于 2024-11-13 04:15:14 字数 177 浏览 2 评论 0原文

有人告诉我必须使用哈希映射来存储单选按钮。 (按钮作为键,它们的状态作为值)但我对 Java 和 Android 很陌生,所以这有点令人困惑。

我有一个包含多个问题和是或否答案的选择测试,我想计算是的答案。

也许这是一些简单的事情,但我无法弄清楚。

任何帮助将不胜感激。

谢谢

I was told that I have to use a hash-map to store radio buttons. (The buttons as the key and their state as value) but I am new to Java and Android so It's kinda confusing.

I have a choice test with multiple questions and yes or no answers and I want to count the yes answers.

Maybe it's some simple thing to do but I can't figure out.

Any help would be appreciated.

Thanks

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

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

发布评论

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

评论(1

薔薇婲 2024-11-20 04:15:14

您可以将对所有对象的引用添加到集合中,并对它们进行迭代,检查它们是否已被检查并对它们进行计数。

或者,您可以在所有按钮上注册一个 onClick 侦听器,并在单击按钮时增加/减少计数。

有多种方法可以实现这一目标。我不明白为什么你需要一个哈希图,因为选中状态在按钮对象本身上可用。你的哈希值没用。

You could add a reference to all of your objects to a collection and iterate over them checking whether they are checked and counting them up.

Alternatively you could register an onClick listener on all of the buttons and increment/decrement a count when they are clicked.

There are multiple ways you could achieve this. I dont see why you need a hash map though, as the checked state is available on the button object itself. Your hash is useless.

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