【问题标题】:How to implement contacts filter in android phonegap?如何在 android phonegap 中实现联系人过滤器?
【发布时间】:2014-07-22 15:51:04
【问题描述】:

我可以选择在我的应用程序中发送短信。所以有一个文本框可以输入手机号码。如果用户输入号码,我需要动态填充过滤后的联系人。我只需要类似于手机联系人的搜索条件。即,如果用户输入 998,我需要根据给定的输入 998 显示过滤后的联系人列表。

为此,我使用了navigator.contacts.find(),使用这种方法我能够获取并找到特定的联系人。但不是动态搜索条件。

这是我的文本框,

 <input name="" id="numberTxt" placeholder="Enter A Mobile Number" value=""  type="tel" data-mini="true">

如果用户在输入时输入任何数字,我需要显示过滤后的数字。这可能吗?如果是,我该怎么做?需要任何额外的插件吗?任何建议,

function onDeviceReady() {
    var options = new ContactFindOptions();
    options.filter = "";         
    options.multiple = true;     
    filter = ["displayName", "name"]; 
    navigator.contacts.find(filter, onSuccess, onError, options);
}

function onSuccess(contacts) {
       for (var i = 0; i < contacts.length; i++) {
            console.log("Display Name = " + contacts[i].displayName);
        }
}

function onError(contactError) {
     alert('onError!');
}

【问题讨论】:

    标签: android cordova phonegap-plugins android-contacts


    【解决方案1】:

    我已经从以下链接安装了 Contact Picker 插件,https://github.com/hazemhagrass/ContactPicker.git 并稍微修改了代码代码,例如,

    window.plugins.ContactPicker.chooseContact(function(contactInfo) {
            var contactNumber = contactInfo.mobileNumber;
            document.getElementById("numberTxt").value= contactNumber ;
        });
    

    我已将mobileNumber 参数添加到contactInfo 对象,以便我能够从通讯录中获取任何联系号码并在HTML 中使用。

    ContactPicker.js 修改代码,

    cordova.exec(function(contactInfo) {
            newContantInfo = {
                displayName: contactInfo.displayName,
                email: contactInfo.email,
                mobileNumber:contactInfo.mobileNumber, //included the mobile Number parameter
                phones: []
            };
            for (var i in contactInfo.phones) {
                if (contactInfo.phones[i].length)
                newContantInfo.phones = newContantInfo.phones.concat(contactInfo.phones[i]);
            };
            success(newContantInfo);
        }, failure, "ContactPicker", "chooseContact", []);
    

    在 ContactPickerPlugin.java 中,

     contact.put("email", email);
        contact.put("displayName", name);
        contact.put("mobileNumber", mobileNumber); // included the mobile Number in contact object
        contact.put("phones", phones);
    

    【讨论】:

      【解决方案2】:

      确实,您可以按电话号码进行过滤。您需要确保“phoneNumbers”字段名称列在要过滤的字段数组中。下面是我在 Android 上测试的按电话号码过滤的示例。

      document.getElementById('searchContactsButton').onclick = function(e){
                var options = new ContactFindOptions();
                options.filter = document.getElementById('searchInput').value;
                options.multiple = true;
                var fieldsToFilter = ["phoneNumbers"];
                navigator.contacts.find(fieldsToFilter, 
                  function(contacts) {
                      alert('found ' + contacts.length + ' contacts');
                }, function(contactError) {
                      alert('error: ' + contactError);
                }, options);
              }
      

      【讨论】:

      • 我不需要检查联系人是否存在。在键入手机号码时按键,我需要显示匹配的模式。就像如果我输入 998 它应该自动显示具有 998 号码的联系人。我该怎么做?有什么想法吗?
      • 您可以尝试将事件处理程序添加到输入元素并监听“更改”或“输入”事件。因此,触发您的功能,将过滤设备上的联系人。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 2011-09-02
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      相关资源
      最近更新 更多