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(); 
     } 
    } 
} 

答えて

0

がでReplaceAllを使用してみてください。例: String phone = cursor.getString(0); phone = phone.replaceAll("+33", ""); editText.setText(phone);

+0

回答ありがとうございますが、動作しません。 –

1

Googleのlibphonenumberライブラリを使用します。フォーマットは場所によって異なりますので、独自のロジックを作成しようとしないでください。

+0

私のケースに合ったコードの例はありますか? –

関連する問題