如何动态加载下拉框,XML内容
<choice> <choiceradiogroup> <_renderer> <![CDATA[<radiobutton><_value>${itemValue}</_value>${itemName}</radiobutton>]]> </_renderer> <_items>@{itemList}</_items> </choiceradiogroup> </choice>
java代码 DataProvide
public class combo implements Frame {
protected Screen screen;
// Associate our data provider to the frame
protected MyDataProvider dataProvider = new MyDataProvider();
public void onAdded() {
// TODO Auto-generated method stub
dataProvider.initializeChoices();
screen = Kuix.loadScreen("combo.xml", dataProvider);
screen.setCurrent();
}
public boolean onMessage(Object identifier, Object[] arg1) {
// TODO Auto-generated method stub
if ("back".equals(identifier)) {
// remove the current frame from the framehandler stack
Kuix.getFrameHandler().removeFrame(this);
// and display the main screen
Kuix.getFrameHandler().getTopFrame().onAdded();
// do not propagate the message through the rest of the frame stack
return false;
}
// let the next frames in the stack, process the message
return true;
}
public void onRemoved() {
// TODO Auto-generated method stub
screen.cleanUp();
// unreference the screen object to free the memory
screen = null;
}
}
class MyDataProvider extends DataProvider {
private static final String ITEM_LIST = "itemList";
public void initializeChoices() {
for(int i=0;i<2;i++) {
ChoiceListItem item = new ChoiceListItem();
item.setItemName("ItemName " + i);
item.setItemValue("ItemValue " + i);
addItem(ITEM_LIST, item);
}
}
}
class ChoiceListItem extends DataProvider{
private String itemValue;
private String itemName;
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemValue() {
return itemValue;
}
public void setItemValue(String itemValue) {
this.itemValue = itemValue;
}
protected Object getUserDefinedValue(String property) {
// handle custom properties requests
if ("itemName".equals(property)) {
return this.itemName;
}
if ("itemValue".equals(property)) {
return this.itemValue;
}
// default behavior if the property has not been found
return null;
}
}
获取下拉框当前选择项的数值
<choice>
<choiceRadiogroup id="chk1">
。。。
<button onAction="choicechange(#chk1.selectedradiobutton)">get</button>
里面的事件处理代码:
RadioButton rad=(RadioButton)arguments[0];
Kuix.alert((String)rad.getValue());
其他说明:
1 onAction是在弹出下拉列表时触发,不是修改选择项后触发的
2 getTag返回的是标签“RadioButton”,由于列表中可能不只包含文字,所以无论ChoiceGroup还是RadioButton都不提供读取当前选中文字的功能,如果确实需要返回里面的文字,可以考虑把文字写入ID,前面加适当前缀,但是必须保证唯一
3 接2,实机上下拉框是一个容器,如果其中只包含文本的话可以用下面的代码获取其中的文本
((Text)rad.getChild()).getText();
4 实际上上面的触发事件用
choicechange(#chk1.value)可以直接传入选中项的值