钛合金ios& amp;自动完成Textfield Android的

时间:2012-12-17 09:35:51

标签: android ios titanium appcelerator-mobile autocompletebox

我想要自动完成文本框,如果我按下它应该显示相关的单词,如苹果,飞机等,我想做类似谷歌搜索。有任何控制像这样的操作在ios和Android上的钛.Help示例代码或钛不可能?

3 个答案:

答案 0 :(得分:3)

以下代码适合您。尝试并根据您的需要进行修改。这里我使用array(searchArray)作为数据存储(您可以将其替换为数据库字段或根据您的要求提供源代码)。

//Table view showing your autocomplete values
var tblvAutoComplete = Ti.UI.createTableView({
    width           : '100%',
    backgroundColor : '#EFEFEF',
    height          : 0,
    maxRowHeight    : 35,
    minRowHeight    : 35,
    allowSelection  : true
});
//Starts auto complete
txtAutoComplete.addEventListener('change', function(e){ 
    var pattern = e.source.value;
    var tempArray = PatternMatch(searchArray, pattern);
    CreateAutoCompleteList(tempArray);
});
//You got the required value and you clicks the word
tblvAutoComplete.addEventListener('click', function(e){
    txtAutoComplete.value = e.rowData.result; 
});

//Returns the array which contains a match with the pattern
function PatternMatch(arrayToSearch, pattern){
    var searchLen = pattern.length;
    arrayToSearch.sort();
    var tempArray = [];
    for(var index = 0, len = arrayToSearch.length; index< len; index++){
        if(arrayToSearch[index].substring(0,searchLen).toUpperCase() === pattern.toUpperCase()){
            tempArray.push(arrayToSearch[index]);
        }
    }
    return tempArray;
}
//setting the tableview values
function CreateAutoCompleteList(searchResults){
    var tableData = [];
    for(var index=0, len = searchResults.length; index < len; index++){

            var lblSearchResult = Ti.UI.createLabel({
                top            : 2,
                width          : '40%',
                height         : 34,
                left           : '5%',
                font           : { fontSize : 14 },
                color          : '#000000',
                text           : searchResults[index]
            });

            //Creating the table view row
            var row = Ti.UI.createTableViewRow({
               backgroundColor : 'transparent',
               focusable       : true,
               height          : 50,
               result          : searchResults[index]
            });

            row.add(lblSearchResult);
            tableData.push(row);
    }
    tblvAutoComplete.setData(tableData);
    tblvAutoComplete.height = tableData.length * 35;
}

此代码在ios和android中都适用于我。希望你的问题得到解决:D

答案 1 :(得分:3)

很酷,我可以用android自动地址建议栏给你一些例子

var search = Titanium.UI.createTextField({
 height : '40sp',
        hintText : 'Search',
        top : '3sp',
        right : '0%',
        width : '73%',
        textAlign : 'left',
        font : {

            fontFamily : 'Verdana',
            fontSize : '13sp',

        },
    });

您的结果显示为表格行

var resulttable = Ti.UI.createTableView({
        top : '0%',
        left : '0%',
        width : '100%',
        height : '100%',
        separatorColor : '#000000',

    });

并添加事件列表器作为更改以及任何带有您的值的更改调用函数,如果值为空,则从视图中删除表

search.addEventListener("change", function(event, type) {
        Titanium.API.info("in change event listener");
        if ('' != search.value) {
            tabgroupContentView.add(resulttable);
            if (resulttable.data.length > 0) {
                for (var i = resulttable.data[0].rows.length - 1; i >= 0; i--) {
                    resulttable.deleteRow(i);
                }
            }
            auto_complete(search.value);
        } else {
            tabgroupContentView.remove(resulttable);
        }
    });

调用以下功能自动完成

function auto_complete(search_term) {

        loader.open("GET", "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" + search_term + "&types=geocode&language=en&sensor=true&key=bar blar blar this is my key use ur one");

        loader.onload = function() {

            var histryresult = eval('(' + this.responseText + ')');
            jsonArry = histryresult.predictions;
            jsonArryterms = histryresult.terms;
            for (var i = 0; i < jsonArry.length; i++) {

                var service_row = Ti.UI.createTableViewRow({
                    height : '70sp',
                    width : '100%',
                    backgroundColor : '#ffffff',
                    backgroundFocusedColor : '#FF8F2F',
                    backgroundSelectedColor : '#FF8F2F',
                    hasChild : false
                });

                var lbl_oderid = Ti.UI.createLabel({
                    left : '3%',
                    top : '10%',
                    text : jsonArry[i].terms[1].value,
                    color : '#A70CAF',
                    font : {
                        fontSize : '17sp',
                        fontWeight : 'bold'
                    },
                    height : 'auto',
                    width : 'auto'
                });

                var descriptiontext;
                if (jsonArry[i].description == undefined) {
                    descriptiontext = 'Not Valable';
                } else {
                    descriptiontext = jsonArry[i].description;
                }

                var lbl_description = Ti.UI.createLabel({
                    left : '5%',
                    top : '50%',
                    text : descriptiontext,
                    color : '#000000',
                    font : {
                        fontSize : '12sp',

                    },
                    height : 'auto',
                    width : 'auto'
                });

                service_row.add(lbl_oderid);
                service_row.add(lbl_description);
                service_row.addEventListener('click', function(e) {

                    var locaName = jsonArry[e.index].description;

                    if (jsonArry[e.index].description == undefined) {

                    } else {
                        reversGeoloader.open("GET", "http://maps.googleapis.com/maps/api/geocode/json?address=" + locaName + "&sensor=false");

                        reversGeoloader.onload = function() {

                            var geoResult = eval('(' + this.responseText + ')');
                            jsonArry = geoResult.results;

                            var newlat = jsonArry[0].geometry.location.lat;
                            var newlng = jsonArry[0].geometry.location.lng;

                            curentlatitude = newlat;
                            curentlongitude = newlng;

                            getReversGeo(curentlatitude, curentlongitude, 'str');
                            usercurentlocation.setText('Set by serch');
                            tabTestWindow.close();

                        };
                        reversGeoloader.send();
                    }

                });

                resulttable.appendRow(service_row);

            }

        };
        loader.send();

    }

答案 2 :(得分:2)

textField.addEventListener('change', function(e) {
    // you can fill a tableView in this event with the suggested data
});

或本教程链接可能会解决您的问题 AutoCompleteTextField