从联系人中选择电话号码

3
我对这一部分有些疑问。如何在从联系人列表获取电话号码时替换字符串中的空格?它已经可以正常工作,但是某些安卓设备(例如三星平板)在他们的联系人中添加了空格。我从联系人中获取号码,所以我得到的字符串是 "99 99 99999" 而不是 "99999999"。
还有,如何从该号码中消除国家代码。例如:"+91 999999999" 而不是 "9999999999" 或 "+020 9696854549" 而不是 "9696854549"。
我知道可以使用.replace()来去除空格。
但是否有其他方法去除字符串之间的空格?
以下是我的代码:
   public void onClick(View view) {
            Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
              startActivityForResult(contactPickerIntent, 
RESULT_PICK_CONTACT);
        }

private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null ;
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        phoneNo = cursor.getString(phoneIndex);

        mobile_et.setText(phoneNo);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

也许你可以使用 https://github.com/googlei18n/libphonenumber 。 - zeekhuge
https://github.com/googlei18n/libphonenumber - MurugananthamS
2个回答

3

String类有一个方法:

.replace(char oldChar, char newChar)

它返回一个新的字符串,该字符串将此字符串中所有出现的 oldChar 替换为 newChar。因此,您只需用空字符串 (即 "") 替换所有空格:
phoneNo = cursor.getString(phoneIndex);
String phoneNumber= str.replaceAll(" ", "");  // your string without any spaces

1

只需替换电话号码中的每个空格。

phoneNo=phoneNo.replaceAll("\\s+","");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接