【问题标题】:Picking a number from a contact but removing the country code从联系人中选择号码但删除国家/地区代码
【发布时间】:2017-08-27 08:36:07
【问题描述】:

我可以通过单击按钮获取联系电话号码,然后将其传输到 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();
        }
    }
}

【问题讨论】:

    标签: android android-edittext number-formatting mobile-country-code


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-18
        • 2011-12-07
        • 2021-07-06
        • 1970-01-01
        相关资源
        最近更新 更多