Android edittext - 选择联系人的电话号码(自动完成)

发布于 2024-10-13 01:42:14 字数 108 浏览 3 评论 0原文

有没有办法放置一个 EditText 元素,让我可以从联系人列表中选择联系人的电话号码。就像通用 Android 短信应用程序一样。 (输入电话号码或联系人姓名时自动完成!)。

Is there a way to put a EditText element in which i could choose contact's phone number from my contacts list. Just as in generic android sms app. (Auto-complete when typing phone number or contact name!).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白龙吟 2024-10-20 01:42:14

您是否查看过 Android 的自动完成示例:http://developer.android .com/resources/tutorials/views/hello-autocomplete.html

也许您可以调整此示例以添加您的联系人列表。

Have you checked out Android's Auto Complete example at : http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

maybe you can tweak this example to add your contact list.

巨坚强 2024-10-20 01:42:14

Hakan的答案很好,但与游标的使用有关。
一般来说,您可以编写自己的适配器,例如通过扩展 ArrayAdapter。
在我的应用程序中,适配器必须做很多“奇怪的事情”,例如在数据库中搜索,然后重新过滤游标,添加其他结果,更改其他...
我做了这样的事情:

public class MyAdapter extends ArrayAdapter {
  private List<String> mObjects; //the "strange Strings"
  private MyHelper dbHelper; // an helper to make query
  private MyFilter mFilter; // my personal filter: this is very important!!
  private final Object mLock=new Object();


  //functions very similar to the ArrayAdapter implementation
@Override
public int getCount() {
return mObjects.size();
}

@Override
public Filter getFilter() {
if (mFilter==null) {
    mFilter=new TeamFilter();
}
return mFilter;
}

@Override
public String getItem(int position) {
return mObjects.get(position);
}

@Override
public int getPosition(String item) {
return mObjects.indexOf(item);
}

  //the trick is here!
  private class MyFilter extends Filter {
    //"constraint" is the string written by the user!
@Override
protected FilterResults performFiltering(CharSequence constraint) {
    FilterResults results=new FilterResults();
        //no constraint => nothing to return
    if ((constraint==null)||(constraint.length()==0)) {
    synchronized (mLock) {
        ArrayList<String> list=new ArrayList<String>();
        results.values=list;
        results.count=list.size();
    }
    }
    else {
    String constr=constraint.toString();

    mObjects= // do what you want to do to populate you suggestion - list
            //( I call the db and change some values)
    results.values=mObjects;
    results.count=mObjects.size();
    }
    return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
    if (results.count>0) {
    notifyDataSetChanged();
    }
    else {
    notifyDataSetInvalidated();
    }
}
  }
}

Hakan's answer is good, but it's related to Cursor use.
In general you can write your own adpater, for instance by extending the ArrayAdapter.
In my application, the Adapter had to do a lot of "strange things" like search in db, then re-filter the Cursors, add other results, changing other...
I did something like this:

public class MyAdapter extends ArrayAdapter {
  private List<String> mObjects; //the "strange Strings"
  private MyHelper dbHelper; // an helper to make query
  private MyFilter mFilter; // my personal filter: this is very important!!
  private final Object mLock=new Object();


  //functions very similar to the ArrayAdapter implementation
@Override
public int getCount() {
return mObjects.size();
}

@Override
public Filter getFilter() {
if (mFilter==null) {
    mFilter=new TeamFilter();
}
return mFilter;
}

@Override
public String getItem(int position) {
return mObjects.get(position);
}

@Override
public int getPosition(String item) {
return mObjects.indexOf(item);
}

  //the trick is here!
  private class MyFilter extends Filter {
    //"constraint" is the string written by the user!
@Override
protected FilterResults performFiltering(CharSequence constraint) {
    FilterResults results=new FilterResults();
        //no constraint => nothing to return
    if ((constraint==null)||(constraint.length()==0)) {
    synchronized (mLock) {
        ArrayList<String> list=new ArrayList<String>();
        results.values=list;
        results.count=list.size();
    }
    }
    else {
    String constr=constraint.toString();

    mObjects= // do what you want to do to populate you suggestion - list
            //( I call the db and change some values)
    results.values=mObjects;
    results.count=mObjects.size();
    }
    return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
    if (results.count>0) {
    notifyDataSetChanged();
    }
    else {
    notifyDataSetInvalidated();
    }
}
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文