`
心雨心
  • 浏览: 352294 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Kuix教程3 - 动态生成下拉框

    博客分类:
  • j2me
阅读更多
如何动态加载下拉框,XML内容



Xml代码
<choice> 
    <choiceradiogroup> 
        <_renderer> 
        <![CDATA[<radiobutton><_value>${itemValue}</_value>${itemName}</radiobutton>]]> 
        </_renderer> 
        <_items>@{itemList}</_items> 
    </choiceradiogroup> 
</choice> 

<choice>
    <choiceradiogroup>
        <_renderer>
        <![CDATA[<radiobutton><_value>${itemValue}</_value>${itemName}</radiobutton>]]>
        </_renderer>
        <_items>@{itemList}</_items>
    </choiceradiogroup>
</choice> 


java代码 DataProvide



Java代码
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;  
    }     


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;
    }
}
 
        获取下拉框当前选择项的数值



Java代码
<choice>  
    <choiceRadiogroup id="chk1">  
。。。  
<button onAction="choicechange(#chk1.selectedradiobutton)">get</button> 

<choice>
<choiceRadiogroup id="chk1">
。。。
<button onAction="choicechange(#chk1.selectedradiobutton)">get</button>     里面的事件处理代码:



Java代码
RadioButton rad=(RadioButton)arguments[0];  
Kuix.alert((String)rad.getValue()); 

RadioButton rad=(RadioButton)arguments[0];
Kuix.alert((String)rad.getValue());     其他说明:

    1 onAction是在弹出下拉列表时触发,不是修改选择项后触发的

    2 getTag返回的是标签“RadioButton”,由于列表中可能不只包含文字,所以无论ChoiceGroup还是RadioButton都不提供读取当前选中文字的功能,如果确实需要返回里面的文字,可以考虑把文字写入ID,前面加适当前缀,但是必须保证唯一

    3 接2,实机上下拉框是一个容器,如果其中只包含文本的话可以用下面的代码获取其中的文本


Java代码
((Text)rad.getChild()).getText(); 

((Text)rad.getChild()).getText();     4 实际上上面的触发事件用choicechange(#chk1.value)可以直接传入选中项的值


转载于:http://shappy1978.iteye.com
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics