1

我可以通过单击按钮获取联系电话号码,然后将其传输到 EditText,但我只想获得没有国家代码的结果。示例:+33 xx xx xx xxx becomes xx xx xx xxx

有没有办法可以做到这一点?我使用以下代码获取数字并将其放入 EditText:

按钮点击:

Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if ((requestCode == 1) && (resultCode == RESULT_OK)) {
        Cursor cursor = null;
        try {
            Uri uri = data.getData();
            cursor = getContentResolver().query(uri, new String[] { CommonDataKinds.Phone.NUMBER }, null, null, null);
            if (cursor != null && cursor.moveToNext()) {
                String phone = cursor.getString(0);
                editText.setText(phone);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
4

2 回答 2

1

使用 Google 的libphonenumber库。不要尝试创建自己的逻辑,因为格式因位置而异。

于 2017-04-02T12:55:51.223 回答
0

尝试使用全部替换。例如: String phone = cursor.getString(0); phone = phone.replaceAll("+33", ""); editText.setText(phone);

于 2017-04-02T12:40:32.713 回答