如何在CodenameOne中创建动态单选按钮 [英] How to create dynamic radiobuttons in CodenameOne
问题描述
使用CodenameOne,我根据数据库表上的记录数创建了一个RadioButtons数组。
容器searchDisplayContainer = findMyContainer(f);
for(int i = 0; i< records.length; i ++){
RadioButton rdb = new RadioButton();
rdb.setName( rdb + i);
rdb.setText( rdb + i);
rdb.setGroup( locations);
searchDisplayContainer.addComponent(rdb);
}
使用此代码,模拟器屏幕上将显示许多RadioButton。 / p>
我的问题是显示单选按钮后,我无法检查选择了哪个按钮。
通常我可以创建
RadioButton rdb1 = findRadioButton(f);然后在theme.res中调用单选按钮。
并检查是否已使用if(rdb1.isSelected)
$ b $选择了按钮b但是,由于我最初不是在theme.res中创建单选按钮,所以不能使用 findRadioButton(f)
方法。
我的问题是如何在代码内创建多个RadioButton,然后在按下提交按钮后能够检查是否选中了它们?
问题修改
包用户类;
导入生成。StateMachineBase;
import com.codename1.ui。*;
import com.codename1.ui.events。*;
import com.codename1.ui.util.Resources;
/ **
*
* @author在这里您的名字
* /
公共类StateMachine扩展StateMachineBase {
私有容器searchDisplayContainer;
private final int recordLength = 5;
private ButtonGroup bg = new ButtonGroup();
private RadioButton [] rdbs = new RadioButton [recordLength];
public StateMachine(String resFile){
super(resFile);
}
/ **
*此方法应用于初始化变量而不是
*构造函数/类范围,以避免出现竞争情况
* /
保护无效initVars(资源res){
}
@Override
保护无效beforeMain(Form f){
searchDisplayContainer = findContainer1(f) ;
for(int i = 0; i rdbs [i] .setName( rdb + i);
rdbs [i] .setText( rdb + i);
//添加到按钮组
bg.add(rdbs [i]);
//添加到容器
searchDisplayContainer.addComponent(rdbs [i]);
}
}
@Override
protected void onMain_ButtonAction(Component c,ActionEvent event){
System.out.println(bg .getSelectedIndex());
}
}
您需要创建 ButtonGroup
类的实例并向其添加单选按钮,以便将其与组中的其他按钮相关联。
Using CodenameOne, I created an array of RadioButtons based on the count of records on a database table.
Container searchDisplayContainer = findMyContainer(f);
for(int i=0; i < records.length; i++) {
RadioButton rdb = new RadioButton();
rdb.setName("rdb"+i);
rdb.setText("rdb"+i);
rdb.setGroup("locations");
searchDisplayContainer.addComponent(rdb);
}
With this code, a number of RadioButtons are displayed on the simulator screen.
My problem is that after displaying the radiobuttons, I cannot check which one is selected.
Usually I can create the radiobutton in the theme.res and call it in the code with:
RadioButton rdb1 = findRadioButton(f);
and check if the button is selected with if(rdb1.isSelected)
However since I am not creating my radio buttons initially in the theme.res, I cannot use the findRadioButton(f)
method.
My question is how do I create a number of RadioButtons within the code and then be able to check if they are selected after the submit button is pressed?
Question Modification
package userclasses;
import generated.StateMachineBase;
import com.codename1.ui.*;
import com.codename1.ui.events.*;
import com.codename1.ui.util.Resources;
/**
*
* @author Your name here
*/
public class StateMachine extends StateMachineBase {
private Container searchDisplayContainer;
private final int recordLength = 5;
private ButtonGroup bg = new ButtonGroup();
private RadioButton[] rdbs = new RadioButton[recordLength];
public StateMachine(String resFile) {
super(resFile);
}
/**
* this method should be used to initialize variables instead of
* the constructor/class scope to avoid race conditions
*/
protected void initVars(Resources res) {
}
@Override
protected void beforeMain(Form f) {
searchDisplayContainer = findContainer1(f);
for(int i=0;i<recordLength;i++){
rdbs[i].setName("rdb"+i);
rdbs[i].setText("rdb"+i);
//add to button group
bg.add(rdbs[i]);
//add to container
searchDisplayContainer.addComponent(rdbs[i]);
}
}
@Override
protected void onMain_ButtonAction(Component c, ActionEvent event) {
System.out.println(bg.getSelectedIndex());
}
}
You need to create an instance of the ButtonGroup
class and add the radio buttons to it in order to associate it to the other buttons in the group.
这篇关于如何在CodenameOne中创建动态单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!