在 Java 中使用微调器中的单选按钮

发布于 2024-11-27 05:43:49 字数 1164 浏览 0 评论 0原文

我正在尝试在微调器中添加一些单选按钮。不过我想只使用 java 来做到这一点。我见过很多例子展示如何使用 xml 资源来做到这一点,但没有像这样的。由于某种原因,从数组获取资源的方法对我不起作用。这是我的代码

            spinnerrg = new Spinner(con);
            NodeList rl = child.getChildNodes();
            String[] radioadapt = new String[rl.getLength()];
            for(int j=1;j<rl.getLength();j++) {
                Element[] dumrad = new Element[rl.getLength()];
                Node dum = rl.item(j);
                if(dum instanceof Element) {
                    dumrad[j] = (Element) dum;
                    radioadapt[j] = dumrad[j].getAttribute("value");
                }
            }
            ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(con, android.R.layout.simple_spinner_item, radioadapt);
            spinnerrg.setAdapter(adapter);
            TextView radioText = new TextView(con);
            radioText.setText(child.getAttribute("label"));
            spinnerrg.setTag(child.getAttribute("id"));
            contList.add(radioText);
            contList.add(spinnerrg);

,nodeList 在那里是因为我正在解析 xml 文件。如果我只是使用 radiogroup 但无法使用 spinner 来实现,则 for-loop 可以完美地工作。

I'm trying to add some radiobuttons in a spinner. However I want to do this by using only java. I've seen a lot of examples showing how to do it with xml resource but nothing like this. Ones getting resource from an array didn't work for me for some reason. Here's my code

            spinnerrg = new Spinner(con);
            NodeList rl = child.getChildNodes();
            String[] radioadapt = new String[rl.getLength()];
            for(int j=1;j<rl.getLength();j++) {
                Element[] dumrad = new Element[rl.getLength()];
                Node dum = rl.item(j);
                if(dum instanceof Element) {
                    dumrad[j] = (Element) dum;
                    radioadapt[j] = dumrad[j].getAttribute("value");
                }
            }
            ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(con, android.R.layout.simple_spinner_item, radioadapt);
            spinnerrg.setAdapter(adapter);
            TextView radioText = new TextView(con);
            radioText.setText(child.getAttribute("label"));
            spinnerrg.setTag(child.getAttribute("id"));
            contList.add(radioText);
            contList.add(spinnerrg);

That nodeList is there because I'm parsing an xml file. for-loop works perfectly if I simply use radiogroup but can't manage to do it with spinner.

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

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

发布评论

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

评论(1

别靠近我心 2024-12-04 05:43:49

看看这个

spinnerrg = new Spinner(con);
            //RadioGroup rg = new RadioGroup(con);
            NodeList rl = child.getChildNodes();
            Vector<String> radioadapt = new Vector<String>();
            for(int j=0;j<rl.getLength();j++) {
                Element[] dumrad = new Element[rl.getLength()];
                Node dum = rl.item(j);
                if(dum instanceof Element) {
                    //RadioButton rb = new RadioButton(con);
                    dumrad[j] = (Element) dum;
                    //rb.setHint(dumrad[j].getAttribute("value"));
                    radioadapt.add(dumrad[j].getAttribute("value"));
                    //rg.addView(rb);

                }
            }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(con, android.R.layout.simple_spinner_item, radioadapt);

            spinnerrg.setAdapter(adapter);
            TextView radioText = new TextView(con);
            radioText.setText(child.getAttribute("label"));
            //spinnerrg.setTag(child.getAttribute("id"));
            contList.add(radioText);
            contList.add(spinnerrg);

Look at this

spinnerrg = new Spinner(con);
            //RadioGroup rg = new RadioGroup(con);
            NodeList rl = child.getChildNodes();
            Vector<String> radioadapt = new Vector<String>();
            for(int j=0;j<rl.getLength();j++) {
                Element[] dumrad = new Element[rl.getLength()];
                Node dum = rl.item(j);
                if(dum instanceof Element) {
                    //RadioButton rb = new RadioButton(con);
                    dumrad[j] = (Element) dum;
                    //rb.setHint(dumrad[j].getAttribute("value"));
                    radioadapt.add(dumrad[j].getAttribute("value"));
                    //rg.addView(rb);

                }
            }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(con, android.R.layout.simple_spinner_item, radioadapt);

            spinnerrg.setAdapter(adapter);
            TextView radioText = new TextView(con);
            radioText.setText(child.getAttribute("label"));
            //spinnerrg.setTag(child.getAttribute("id"));
            contList.add(radioText);
            contList.add(spinnerrg);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文