[转]kuix 教程 下拉框 动态

  如何动态加载下拉框,XML内容

 

Xml代码
  1. <choice>  
  2.     <choiceradiogroup>  
  3.         <_renderer>  
  4.         <![CDATA[<radiobutton><_value>${itemValue}</_value>${itemName}</radiobutton>]]>  
  5.         </_renderer>  
  6.         <_items>@{itemList}</_items>  
  7.     </choiceradiogroup>  
  8. </choice>  
 

 

java代码 DataProvide

 

Java代码
  1. public class combo implements Frame {   
  2.     protected Screen screen;   
  3.     // Associate our data provider to the frame   
  4.     protected MyDataProvider dataProvider = new MyDataProvider();   
  5.   
  6.     public void onAdded() {   
  7.         // TODO Auto-generated method stub   
  8.         dataProvider.initializeChoices();   
  9.         screen = Kuix.loadScreen("combo.xml", dataProvider);   
  10.         screen.setCurrent();   
  11.     }   
  12.   
  13.     public boolean onMessage(Object identifier, Object[] arg1) {   
  14.         // TODO Auto-generated method stub   
  15.         if ("back".equals(identifier)) {   
  16.             // remove the current frame from the framehandler stack   
  17.             Kuix.getFrameHandler().removeFrame(this);   
  18.   
  19.             // and display the main screen   
  20.             Kuix.getFrameHandler().getTopFrame().onAdded();   
  21.   
  22.             // do not propagate the message through the rest of the frame stack   
  23.             return false;   
  24.         }   
  25.   
  26.         // let the next frames in the stack, process the message   
  27.         return true;   
  28.     }   
  29.   
  30.     public void onRemoved() {   
  31.         // TODO Auto-generated method stub   
  32.         screen.cleanUp();   
  33.         // unreference the screen object to free the memory   
  34.         screen = null;   
  35.     }   
  36.   
  37. }   
  38.   
  39. class MyDataProvider extends DataProvider {   
  40.     private static final String ITEM_LIST = "itemList";   
  41.   
  42.     public void initializeChoices() {   
  43.         for(int i=0;i<2;i++) {   
  44.             ChoiceListItem item = new ChoiceListItem();   
  45.             item.setItemName("ItemName " + i);   
  46.             item.setItemValue("ItemValue " + i);   
  47.             addItem(ITEM_LIST, item);   
  48.         }   
  49.     }   
  50. }   
  51.   
  52. class ChoiceListItem extends DataProvider{   
  53.     private String itemValue;   
  54.     private String itemName;   
  55.     public String getItemName() {   
  56.         return itemName;   
  57.     }   
  58.     public void setItemName(String itemName) {   
  59.         this.itemName = itemName;   
  60.     }   
  61.     public String getItemValue() {   
  62.         return itemValue;   
  63.     }   
  64.     public void setItemValue(String itemValue) {   
  65.         this.itemValue = itemValue;   
  66.     }   
  67.        
  68.     protected Object getUserDefinedValue(String property) {   
  69.         // handle custom properties requests   
  70.         if ("itemName".equals(property)) {   
  71.             return this.itemName;   
  72.         }   
  73.         if ("itemValue".equals(property)) {   
  74.             return this.itemValue;   
  75.         }   
  76.   
  77.         // default behavior if the property has not been found   
  78.         return null;   
  79.     }      
  80. }  
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代码
  1. <choice>   
  2.     <choiceRadiogroup id="chk1">   
  3. 。。。   
  4. <button onAction="choicechange(#chk1.selectedradiobutton)">get</button>  
<choice>
	<choiceRadiogroup id="chk1">
。。。
<button onAction="choicechange(#chk1.selectedradiobutton)">get</button>

     里面的事件处理代码:

 

Java代码
  1. RadioButton rad=(RadioButton)arguments[0];   
  2. 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代码
  1. ((Text)rad.getChild()).getText();  
((Text)rad.getChild()).getText();
     4 实际上上面的触发事件用 choicechange(#chk1.value)可以直接传入选中项的值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值