如何删除国家代码,当从联系人中挑选电话号码时
问题描述:
我对该部分有疑问。当我从联系人列表中选择电话号码时如何删除国家/地区代码?
例如:+91 999999999而不是9999999999或+020 9696854549而不是9696854549任何人都可以知道关于我的问题的答案。请给这个问题的解决办法
我附上我的代码和图像在这里。
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);
String phoneNumber = phoneNo.replaceAll(" ","");
mobile_et.setText(phoneNumber);
} catch (Exception e) {
e.printStackTrace();
}
}
答
使用此功能希望这会帮助你:
public String phoeNumberWithOutCountryCode(String phoneNumberWithCountryCode) {
Pattern complie = Pattern.compile(" ");
String[] phonenUmber = complie.split(phoneNumberWithCountryCode);
Log.e("number is", phonenUmber[1]);
return phonenUmber[1];
}
+0
如果你得到你的解决方案,请upvote我的答案 –
答
我的英语很差,但我会尽我所能来回答你的问题。
首先,这一行添加到您的build.gradle
compile 'com.googlecode.libphonenumber:libphonenumber:8.7.0'
而且下面是科特林
fun deleteCountry(phone: String) : String{
val phoneInstance = PhoneNumberUtil.getInstance()
try {
val phoneNumber = phoneInstance.parse(phone, null)
return phoneNumber?.nationalNumber?.toString()?:phone
}catch (_ : Exception) {
}
return phone
}
样本写。如果电话号码不与“+”启动然后输入国家代码,然后输入地区信息,例如:
VAL phoneNumber的= phoneInstance.parse(电话, “CN”)
答
您可以使用startsWith()
这个方法有两种变体和测试一个字符串是否以 指定的前缀开头的开始指定索引或默认在 开头。
if(phoneNumber.startsWith("+"))
{
if(phoneNumber.length()==13)
{
String str_getMOBILE=phoneNumber.substring(3);
mobile_et.setText(str_getMOBILE);
}
else if(phoneNumber.length()==14)
{
String str_getMOBILE=phoneNumber.substring(4);
mobile_et.setText(str_getMOBILE);
}
}
else
{
mobile_et.setText(phoneNumber);
}
答
它将为所有实例工作。你不必关心国家代码有多少位是
String get_Mo = phoneNumber.substring(phoneNumber.lastIndexOf(' ')+1));
@IntelliJAmiya这将仅适用于有三个国家代码的国家工作数字。美国有1,很多国家有2 ... –
没有什么说所有的电话号码必须有一个国家代码(在联系人列表中)。但是,您可以通过查找“+”来检查它,然后删除它后面的数字(直到第一个空格)。 –
@PeterAbolins确实 –