Android 只能选择电话号码

Android 只能选择电话号码,android,sms,Android,Sms,嗨,我有以下代码。现在我只需要把电话号码输入自动完成文本框…怎么做 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPeopleList = new ArrayList<Map<String, String>>()

嗨,我有以下代码。现在我只需要把电话号码输入自动完成文本框…怎么做

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_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);
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mpeoplist=newarraylist();
PopulatePeopleList();
mTxtPhoneNo=(AutoCompleteTextView)findviewbyd(R.id.mmWhoNo);
mAdapter=new simpledapter(这个,mpeoplist,R.layout.custcontview,
新字符串[]{“Name”,“Phone”,“Type”},新int[]{R.id.ccontName,R.id.ccontNo,R.id.ccontType});
mTxtPhoneNo.setAdapter(mAdapter);
}
public void PopulatePeopleList(){
mpeoplist.clear();
Cursor people=getContentResolver().query(
Contacts contract.Contacts.CONTENT_URI,null,null,null,null);
while(people.moveToNext()){
String contactName=people.getString(人
.getColumnIndex(Contacts contract.Contacts.DISPLAY_NAME));
String contactId=people.getString(人)
.getColumnIndex(Contacts contract.Contacts._ID));
字符串hasPhone=people
.getString(人)
.getColumnIndex(Contacts contract.Contacts.HAS_PHONE_NUMBER));
if((Integer.parseInt(hasPhone)>0)){
//你知道这个数字,所以现在像这样查询它
Cursor=getContentResolver().query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
无效的
Contacts contract.CommonDataTypes.Phone.CONTACT\u ID
+“=”+联系人ID,空,空);
while(phones.moveToNext()){
//存储数字并显示一个对话框,允许用户
//选择哪个。
字符串phoneNumber=电话
.getString(电话)
.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
字符串numberType=电话
.getString(电话)
.getColumnIndex(ContactsContract.CommonDataTypes.Phone.TYPE));
Map NamePhoneType=newhashmap();
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”);
其他的
NamePhoneType.put(“Type”、“Other”);
//然后将此地图添加到列表中。
mpeoplist.add(NamePhoneType);
}
电话。关闭();
}
}
人;
开始管理光标(人);
}
}

它以这种格式返回Name=xyz.Type=Mobile,Number=1234。。。。 我只需要获取号码,并将该号码用于我的程序的后续操作,在该程序中添加代码以获取号码。

*您可以尝试此代码*

仅使用此行

String phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


            NamePhoneType.put("Phone", phoneNumber);

您正在放置所有数据,包括名称、类型。。只需放入电话即可查看

而非输入类型。。他要求在自动完成中显示数字
String phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


            NamePhoneType.put("Phone", phoneNumber);