与本机短信应用程序 Android 中的名称和号码自动完成
Posted
技术标签:
【中文标题】与本机短信应用程序 Android 中的名称和号码自动完成【英文标题】:AutoComplete with name and number as in native sms app Android 【发布时间】:2012-06-24 05:28:14 【问题描述】:我想在我的应用程序中添加一个AutoCompleteTextView
并按姓名和号码搜索联系人,就像在安卓的原生短信应用程序中所做的那样。我在互联网上查看并尝试了很多东西,但我希望我的应用程序将其完全显示为 android SMS 应用程序。这是我正在尝试仅通过 Display_Name
搜索的代码。
public class MakePayment extends Activity
private AutoCompleteTextView mAuto;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.makepayment);
mAuto = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewTest);
ContentResolver content = getContentResolver();
Cursor cursor = content.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PEOPLE_PROJECTION, null, null, null);
ContactListAdapter adapter = new ContactListAdapter(this, cursor);
mAuto.setAdapter(adapter);
public static class ContactListAdapter extends CursorAdapter implements Filterable
public ContactListAdapter(Context context, Cursor c)
super(context, c);
mContent = context.getContentResolver();
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view = (TextView) inflater.inflate(
android.R.layout.simple_expandable_list_item_1, parent, false);
view.setText(cursor.getString(2));
return view;
@Override
public void bindView(View view, Context context, Cursor cursor)
((TextView) view).setText(cursor.getString(2));
@Override
public String convertToString(Cursor cursor)
return cursor.getString(2);
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint)
if (getFilterQueryProvider() != null)
return getFilterQueryProvider().runQuery(constraint);
StringBuilder buffer = null;
String[] args = null;
if (constraint != null)
buffer = new StringBuilder();
buffer.append("UPPER(");
buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
buffer.append(") GLOB ?");
args = new String[] constraint.toString().toUpperCase() + "*" ;
return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PEOPLE_PROJECTION,
buffer == null ? null : buffer.toString(), args,
null);
private ContentResolver mContent;
private static final String[] PEOPLE_PROJECTION = new String[]
ContactsContract.Contacts._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME,
;
我如何也可以通过电话号码进行搜索?然后像这样显示它:
【问题讨论】:
***.com/questions/37724842/… 【参考方案1】:过去我已经做过类似的事情:
注意:点击两个字符后会自动完成
布局文件使用 AutoCompleteView,它基本上是一个带有下拉列表的 EditText,在您输入时会出现。你示例中的 .xml 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_
>
<AutoCompleteTextView
android:id="@+id/mmWhoNo"
android:layout_
android:layout_
android:hint="To...."
>
</AutoCompleteTextView>
</LinearLayout>
要创建 AutoCompleteView 中使用的自定义视图,您必须声明另一个名为 custcontview.xml 的 .xml 文件,它看起来像这样:
<TextView
android:id="@+id/ccontName"
android:layout_
android:layout_
android:text="Large Text"
android:textColor="#A5000000"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/ccontNo"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_below="@id/ccontName"
android:text="Medium Text"
android:textColor="#A5000000"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/ccontType"
android:layout_
android:layout_
android:layout_above="@id/ccontNo"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="Small Text"
android:textColor="#A5000000"
android:textAppearance="?android:attr/textAppearanceSmall" />
现在在您的活动中:
public class ContactActivity extends Activity
private ArrayList<Map<String, String>> mPeopleList;
private SimpleAdapter mAdapter;
private AutoCompleteTextView mTxtPhoneNo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPeopleList = new ArrayList<Map<String, String>>();
PopulatePeopleList();
mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview ,new String[] "Name", "Phone" , "Type" , new int[] R.id.ccontName, R.id.ccontNo, R.id.ccontType );
mTxtPhoneNo.setAdapter(mAdapter);
public void PopulatePeopleList()
mPeopleList.clear();
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext())
String contactName = people.getString(people.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String contactId = people.getString(people.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = people.getString(people.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ((Integer.parseInt(hasPhone) > 0))
// You know have the number so now query it like this
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while (phones.moveToNext())
//store numbers and display a dialog letting the user select which.
String phoneNumber = phones.getString(
phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
String numberType = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.TYPE));
Map<String, String> NamePhoneType = new HashMap<String, String>();
NamePhoneType.put("Name", contactName);
NamePhoneType.put("Phone", phoneNumber);
if(numberType.equals("0"))
NamePhoneType.put("Type", "Work");
else
if(numberType.equals("1"))
NamePhoneType.put("Type", "Home");
else if(numberType.equals("2"))
NamePhoneType.put("Type", "Mobile");
else
NamePhoneType.put("Type", "Other");
//Then add this map to the list.
mPeopleList.add(NamePhoneType);
phones.close();
people.close();
startManagingCursor(people);
别忘了添加
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
在您的清单中
【讨论】:
工作得很好!谢谢你。只有一个问题:当我选择一个特定的联系人时,AutoCompleteTextView 是这样的Name=ABC, Type=XYZ, Phone=xxx-xxx-xxxx。我怎样才能得到它的名字? 你有什么解决方案只显示联系人姓名或号码吗? 为 AutoCompleteTextView 添加一个 onItemClickListener 而不是将它作为一个单独的函数...... mTxtPhoneNo.setOnItemClickListener(new OnItemClickListener() @Override public void onItemClick(AdapterView> av,查看 arg1, int index, long arg3) MapMultiAutoCompleteTextView
,但它不允许多个条目。介意给我一些帮助吗?这是我原来的问题***.com/questions/22443791/… 的链接。描述已演变为您目前的答案。
@K_Anas 嗨,我使用了上述方法,但是如果我在字母中留出空间而不是停止显示建议?知道如何解决这个问题吗?谢谢【参考方案2】:
AutoCompleteTextView 是使用@K_Anas 的代码完成的。由于它返回一个 (key, value) 对,所以我使用以下内容仅显示所选项目的编号:
@Override
public void onItemClick(AdapterView<?> av, View v, int index, long arg)
Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
Iterator<String> myVeryOwnIterator = map.keySet().iterator();
while(myVeryOwnIterator.hasNext())
String key=(String)myVeryOwnIterator.next();
String value=(String)map.get(key);
mTxtPhoneNo.setText(value);
);
本题取得的成果:
AutoCompleteTextView 以与 上面问题中的图片。 将文本设置为所选值的电话号码。【讨论】:
我也和你一样面临同样的问题,当我点击列表中的任何一个数字时,我会得到类似的东西。Name=John, Type=Mobile, Phone=1-234-456-7890
以上是关于与本机短信应用程序 Android 中的名称和号码自动完成的主要内容,如果未能解决你的问题,请参考以下文章
特殊的链接:打电话,短信,email;iPhone 和Android应用