【问题标题】:How to create radio button in Titanium Alloy?如何在钛合金中创建单选按钮?
【发布时间】:2014-09-22 14:21:44
【问题描述】:

我们如何在钛合金框架中创建单选按钮和多选按钮?是否有任何 XML 标签可以创建相同的标签?

我尝试从您的参考和以下链接中添加代码 https://github.com/Lukic/TiRadioButtonGroup/blob/master/Resources/app.js 现在我可以获得单选按钮。但是格式有问题。现在我得到了单选按钮,如屏幕截图所示。

但我无法在屏幕截图中看到任何标签名称作为选项。我也尝试了多个单选按钮,但我遇到了样式问题。我需要类似于以下屏幕截图。我该怎么做。

【问题讨论】:

    标签: titanium titanium-mobile titanium-alloy


    【解决方案1】:
        Although titanium does not support the check box,radio button you could always fake the functionality here is custom check box and radio button for you
    
        var checkbox = Ti.UI.createButton({
        title: '',
        top: 10,
        right: 10,
        width: 30,
        height: 30,
        borderColor: '#666',
        borderWidth: 2,
        borderRadius: 3,
        backgroundColor: '#aaa',
        backgroundImage: 'none',
        color: '#fff',
        font:{fontSize: 25, fontWeight: 'bold'},
        value: false //value is a custom property in this casehere.
    });
    
    //Attach some simple on/off actions
    checkbox.on = function() {
        this.backgroundColor = '#007690';
        this.title='\u2713';
        this.value = true;
    };
    
    checkbox.off = function() {
        this.backgroundColor = '#aaa';
        this.title='';
        this.value = false;
    };
    
    checkbox.addEventListener('click', function(e) {
        if(false == e.source.value) {
            e.source.on();
        } else {
            e.source.off();
        }
    });
    

    【讨论】:

    • 非常漂亮的小复选框。谢谢。作为模块/类可能会更有用,然后它可以更容易地打包并在任何项目中用作 lib。但很惊讶这不是公认的答案。
    【解决方案2】:

    ,在钛合金框架中,我们没有任何像单选按钮这样的对象。为此,我们需要为单选按钮等句柄创建一个函数。

    例如
    我们可以使用 4 个按钮并将图像设置为单选按钮图像(未选择)。

    当我们单击任何一个按钮时,然后更改按钮 带有选择单选按钮的按钮图像。并使用未选择的图像更新所有剩余的按钮图像。逻辑上。

    谢谢,

    【讨论】:

    • 只是一个澄清。我相信钛合金没有它的原因是因为IOS开发工具本身也不支持它。 Titanium 正在将功能映射到本机功能。
    • @Martin 单选按钮在 iOS 上称为 Switch 按钮,Titanium 支持。
    【解决方案3】:

    此代码创建一个单选按钮组。你可以在你的 Alloy 项目中使用它,尽管它不是一个 Alloy Widget。此链接有一个完整的经典示例。

    https://github.com/yozef/TiRadioButtonGroup

    由于它不是 Alloy 小部件,我相信您需要通过 index.js 文件来完成所有这些操作。

    在项目的 app 文件夹中。将 radioButtonActive.png 和 radioButton.png 复制到 assets/iphone 和 assets/android。

    在您的 index.xml 文件中,我通常会执行以下操作:

     <Alloy>
        <Window class="container">
            <View id="radioView" />
        </Window>
    </Alloy>
    

    index.js

    var radioButton = require('tiRadioButton');
    
    var radioGroup = radioButton.createGroup({
        groupId:1,
        width:119,
        height:34,
        layout:'horizontal',
        radioItemsValue:['One', 'Two', 'Three'],
        radioItemsPadding:10,
        radioItemsBackgroundSelectedImage:'radioButtonActive.png',
        radioItemsBackgroundImage:'radioButton.png',
        radioItemsWidth:33,
        radioItemsHeight:34
    });
    
    var headline3 = Ti.UI.createLabel({
        text:'Layout: vertical',
        color:'#fff',
        font:{fontSize:20,fontWeight:'Bold'},
        shadowColor:'#000',
        shadowOffset:{x:1,y:1},
        top:10,
        textAlign:'center'
    });     
    
    var button = Ti.UI.createButton({
        title:'Get value' 
    });
    
    button.addEventListener('singletap', function(e) {
        alert("Horizontal radioGroup selectedIdx: " + radioGroup.selectedValue);
    });
    
    $.radioView.add(radioGroup);
    $.radioView.add(button);
    $.index.open();
    

    index.tss

    ".container": {
        backgroundColor:"white"
    },
    "Label": {
        width: Ti.UI.SIZE,
        height: Ti.UI.SIZE,
        color: "#000"
    }
    

    【讨论】:

    • 我试过了,但是单选按钮 UI 没有显示。请给我举个例子
    • 这是我在测试项目中使用的所有代码。包括我创建的所有文件以及从原始经典项目中获取文件的一些说明。期望在白屏上看到读取 3 个字段的 Get Value 按钮。这些字段没有标签,只有单选按钮。您必须查看单选按钮库以查看它是否处理标签。
    • 我用垂直视图试过你的代码。现在显示单选按钮。但是选项标签没有显示,而且当我尝试添加多个单选按钮问题时,我遇到了样式问题
    • 我已经更新了我的问题,请给我一些答案
    • 虽然我接受了您的回答,但标签并未显示给我。请帮助我,我需要显示选项一、二和三。请帮帮我。
    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多