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

    如何动态加载下拉框,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)可以直接传入选中项的值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用 jQuery 插件 jquery-multiselect 和 jQuery 动态生成下拉框的示例代码: HTML 代码: ```html <div id="selectBoxContainer"></div> ``` JavaScript 代码: ```javascript $(document).ready(function() { // 动态生成下拉框 var selectBox = $('<select id="selectBox" multiple></select>'); selectBox.append($('<option value="option1">选项1</option>')); selectBox.append($('<option value="option2">选项2</option>')); selectBox.append($('<option value="option3">选项3</option>')); selectBox.append($('<option value="option4">选项4</option>')); selectBox.append($('<option value="option5">选项5</option>')); $('#selectBoxContainer').append(selectBox); // 初始化下拉框 $('#selectBox').multiselect(); }); ``` 在上面的示例代码中,我们首先在 HTML 中定义了一个 ID 为 "selectBoxContainer" 的容器,用于放置动态生成下拉框。 然后,在 JavaScript 中,我们使用 jQuery 的 ready() 方法来确保文档加载完成后再执行代码。在 ready() 方法中,我们使用 jQuery 动态生成了一个 ID 为 "selectBox" 的下拉框,并设置了 multiple 属性,表示该下拉框可以多选。接着,我们为下拉框添加了五个选项。 最后,我们将动态生成下拉框添加到容器中,并使用 multiselect() 方法初始化下拉框,从而实现动态生成下拉框并使用 jquery-multiselect 插件实现下拉框多选的功能。 这样做的好处是可以根据实际需求动态生成下拉框,并且可以使用 jquery-multiselect 插件方便地实现下拉框多选的功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值