繁体   English   中英

如何在Appcelerator中使用父子关系更新图像视图的图像?

[英]How to update image of image view using parent child relationship in Appcelerator?

我已经在iOS应用中创建了单选按钮,如下图所示。 我想在用户单击任意单选按钮后立即将其余单选按钮的图像从选中更改为未选中。

下面是我的事件监听器代码。

       vwBottomContainer.addEventListener("click", function(e) {
            Ti.API.info('e.source.id: ' + e.source.id);
            if (e.source.id == "CheckBox") {
                if (e.source.status == "unselected") {
                    e.source.status = "selected";
                    e.source.image = "/images/checked.png";
                } else {
                    e.source.status = "unselected";
                    e.source.image = "/images/unchecked.png";
                }
            } else if (e.source.id == "TextBox") {
                e.source.editable = true;
                e.source.focus();
            } else if(e.source.id == "radiobutton") {
                var t = null;
                for(t = 0 ; t < e.source.parent.parent.children.length; t++) {
                    e.source.parent.parent.children[t].children[0].image = "/images/radio-button_ori.png";
                    e.source.parent.parent.children[t].children[0].status = "unselected";
                    Ti.API.info('e.source: '+JSON.stringify(e.source.parent.parent.children[t].children[0]));
                }
                t = null;

                e.source.image = "/images/radio-button_active.png";
                e.source.status = "selected";
            }
        });

代码中的for循环无法将其他单选按钮的图像从选中更改为未选中

你能告诉我我做错了什么吗? 如果您需要进一步的澄清,也请告诉我。

谢谢Advvance。

在此处输入图片说明

如果您使用的是ListView,则应使用updateItemAt方法。

从Appcelerator文档( http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.ListItem )中看一下此示例:

$.listView.addEventListener('itemclick', function(e) {
    var item = $.section.getItemAt(e.itemIndex);
    if (item.properties.accessoryType == Ti.UI.LIST_ACCESSORY_TYPE_NONE) {
        item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK;
        item.properties.color = 'red';
    } else {
        item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_NONE;
        item.properties.color = 'black';
    }
    $.section.updateItemAt(e.itemIndex, item);
});

您确定嵌套正确吗? 因此e.source.parent.parent.children[t].children[0].image = "/images/radio-button_ori.png"; 要更改单选按钮吗?

尝试不同的设置:

您在使用合金吗? 然后,为图像提供一个自己的ID,然后直接访问即可。

如果不是,请执行以下操作:将所有单选按钮图像推入数组,在vwBottomContainer中添加一个位置,然后从数组中访问单选按钮:

var rbts = [];
rbts.push(radioButtonImage1);
rbts.push(radioButtonImage2);
rbts.push(radioButtonImage3);
outerViewWithClickEvent1.radioID = 0;
outerViewWithClickEvent2.radioID = 1;
outerViewWithClickEvent3.radioID = 2;

在onClick事件中:

rbts[e.source.radioID].image = "newImage.jpg";

如果显示单选按钮的代码/视图,也将有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM