【问题标题】:How to get current radio button index value from a radio button group in codename one?如何从代号一的单选按钮组中获取当前单选按钮索引值?
【发布时间】:2017-04-05 09:22:45
【问题描述】:

我有一些单选按钮放在单选按钮组中。 那么,当我要单击特定单选按钮时,如何获取单选按钮索引值或选定索引值。

这是我的代码

 bg=new ButtonGroup();
 for(i=0;i<8;i++){
 rbt =new RadioButton();
 rbt.setName("rbt"+i);
 radioList.add(rbt);
 }
 for(int i=0;i<radioList.size();i++){
  radioList.get(i).addPointerPressedListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent evt) {
                            for(int i=0;i<radioList.size();i++){
                                  Log.p("================"+bg.getSelectedIndex);
                            }
                        }
                    }); 
 }

在上面的代码中,我得到的是先前选择的值而不是当前值。价值

提前致谢

【问题讨论】:

    标签: codenameone


    【解决方案1】:

    pointerPressed 在单选按钮处理事件之前被调用。改用addActionListener,它在状态改变后触发,所以选中的索引应该是当前选中的单选按钮。

    【讨论】:

      【解决方案2】:

      您应该在容器中添加单选按钮以及按钮组,并调用按钮组的 getselectedIndex 以获取当前索引,如下代码所示

      void testRadio(Form form) {
              Container radioList = new Container(new BoxLayout(BoxLayout.Y_AXIS));
              ButtonGroup bg = new ButtonGroup();
              for (int i = 0; i < 8; i++) {
                  RadioButton rbt = new RadioButton();
                  rbt.setName("rbt" + i);
                  rbt.setText("rbt" + i);
                  **radioList.add(rbt);
                  bg.add(rbt);**
              }
              bg.setSelected(0);
              form.add(radioList);
              Button getIndexButton = new Button("Get Radio Index");
              getIndexButton.addActionListener(new ActionListener() {
      
                  @Override
                  public void actionPerformed(ActionEvent evt) {
      //                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                      Log.p("================" + bg.getSelectedIndex());
                  }
              });
              form.add(getIndexButton);
      
          }
      

      【讨论】:

        猜你喜欢
        • 2014-03-27
        • 2016-02-21
        • 1970-01-01
        • 1970-01-01
        • 2017-02-06
        • 2011-03-14
        • 1970-01-01
        • 2013-12-06
        • 1970-01-01
        相关资源
        最近更新 更多