Android 在安卓系统中,如何将国家代码与电话号码分开?

Android 在安卓系统中,如何将国家代码与电话号码分开?,android,Android,我有一个国家代码及其对应国家的XML列表,可以随时使用,但首先我需要将它们与电话号码分开。在Android API中是否有一种简单的方法可以做到这一点?Java的String.substring()可能对这个用例很有用: yourString.substring(int beginIndex, int endIndex); 您可能需要首先取出XML列表,将用户输入的前3个数字作为子字符串,然后将其与列表的条目进行比较。Java的String.substring()可能对此用例有用: yourS

我有一个国家代码及其对应国家的XML列表,可以随时使用,但首先我需要将它们与电话号码分开。在Android API中是否有一种简单的方法可以做到这一点?

Java的
String.substring()
可能对这个用例很有用:

yourString.substring(int beginIndex, int endIndex);

您可能需要首先取出
XML
列表,将用户输入的前3个数字作为子字符串,然后将其与列表的条目进行比较。

Java的
String.substring()
可能对此用例有用:

yourString.substring(int beginIndex, int endIndex);

您可能需要首先取出
XML
列表,将用户输入的前3个数字作为子字符串,然后将其与列表的条目进行比较。

Java的
String.substring()
可能对此用例有用:

yourString.substring(int beginIndex, int endIndex);

您可能需要首先取出
XML
列表,将用户输入的前3个数字作为子字符串,然后将其与列表的条目进行比较。

Java的
String.substring()
可能对此用例有用:

yourString.substring(int beginIndex, int endIndex);

您可能需要先取出
XML
列表,将用户输入的前3个数字串成子串,然后将其与列表中的条目进行比较。

请签出libphonenumber


这是谷歌的通用java、C++和JavaScript库,用于解析、格式化、存储和验证国际电话号码。Java版本是为在智能手机上运行而优化的,自4.0(冰激凌三明治)以来,Android框架一直在使用它。

请查看libphonenumber


这是谷歌的通用java、C++和JavaScript库,用于解析、格式化、存储和验证国际电话号码。Java版本是为在智能手机上运行而优化的,自4.0(冰激凌三明治)以来,Android框架一直在使用它。

请查看libphonenumber


这是谷歌的通用java、C++和JavaScript库,用于解析、格式化、存储和验证国际电话号码。Java版本是为在智能手机上运行而优化的,自4.0(冰激凌三明治)以来,Android框架一直在使用它。

请查看libphonenumber


这是谷歌的通用java、C++和JavaScript库,用于解析、格式化、存储和验证国际电话号码。Java版本是为在智能手机上运行而优化的,自4.0(冰激凌三明治)以来,Android框架一直在使用它。

我用ridsatrio的答案回答了一个老问题

我使用以下类从下面的字符串数组中获取国家代码:

import android.widget.TextView;

/**
 * Created by Friso on 14/11/21.
 */
public final class PhoneFunctions {
    private static PhoneFunctions instance;

    private PhoneFunctions(){}

    public static PhoneFunctions getInstance() {
        if (instance == null) {
            instance = new PhoneFunctions();
        }

        return instance;
    }

    public String getCountry(String[] argStringArray, TextView argText){
        String country="";

        if (argText.getText().toString().length() >= 4){
            for(int i=0;i<argStringArray.length;i++){
                String[] g=argStringArray[i].split(",");
                if(g[0].equals(getFirstFourChar(argText))){
                    country=g[1];
                    break;
                }
                if (g[0].equals(getFirstThreeChar(argText))){
                    country=g[1];
                    break;
                }
                if (g[0].equals(getFirstTwoChar(argText))){
                    country=g[1];
                    break;
                }
            }
        }

        return country;
    }

    public String getFirstFourChar(TextView argText){
        String threeChar;
        String text = argText.getText().toString();
        threeChar = text.substring(0,4);

        return threeChar;
    }

    public String getFirstThreeChar(TextView argText){
        String twoChar;
        String text = argText.getText().toString();
        twoChar = text.substring(0,3);

        return twoChar;
    }

    public String getFirstTwoChar(TextView argText){
        String oneChar;
        String text = argText.getText().toString();
        oneChar = text.substring(0,2);

        return oneChar;
    }
}
导入android.widget.TextView;
/**
*弗里索于21年11月14日创建。
*/
公共最终类函数{
私有静态电话功能实例;
专用PhoneFunctions(){}
公共静态函数getInstance(){
if(实例==null){
实例=新的PhoneFunctions();
}
返回实例;
}
公共字符串getCountry(字符串[]argStringArray,TextView argText){
字符串country=“”;
如果(argText.getText().toString().length()>=4){

对于(inti=0;i我使用了ridsatrio的答案和一个更老的问题

我使用以下类从下面的字符串数组中获取国家代码:

import android.widget.TextView;

/**
 * Created by Friso on 14/11/21.
 */
public final class PhoneFunctions {
    private static PhoneFunctions instance;

    private PhoneFunctions(){}

    public static PhoneFunctions getInstance() {
        if (instance == null) {
            instance = new PhoneFunctions();
        }

        return instance;
    }

    public String getCountry(String[] argStringArray, TextView argText){
        String country="";

        if (argText.getText().toString().length() >= 4){
            for(int i=0;i<argStringArray.length;i++){
                String[] g=argStringArray[i].split(",");
                if(g[0].equals(getFirstFourChar(argText))){
                    country=g[1];
                    break;
                }
                if (g[0].equals(getFirstThreeChar(argText))){
                    country=g[1];
                    break;
                }
                if (g[0].equals(getFirstTwoChar(argText))){
                    country=g[1];
                    break;
                }
            }
        }

        return country;
    }

    public String getFirstFourChar(TextView argText){
        String threeChar;
        String text = argText.getText().toString();
        threeChar = text.substring(0,4);

        return threeChar;
    }

    public String getFirstThreeChar(TextView argText){
        String twoChar;
        String text = argText.getText().toString();
        twoChar = text.substring(0,3);

        return twoChar;
    }

    public String getFirstTwoChar(TextView argText){
        String oneChar;
        String text = argText.getText().toString();
        oneChar = text.substring(0,2);

        return oneChar;
    }
}
导入android.widget.TextView;
/**
*弗里索于21年11月14日创建。
*/
公共最终类函数{
私有静态电话功能实例;
专用PhoneFunctions(){}
公共静态函数getInstance(){
if(实例==null){
实例=新的PhoneFunctions();
}
返回实例;
}
公共字符串getCountry(字符串[]argStringArray,TextView argText){
字符串country=“”;
如果(argText.getText().toString().length()>=4){

对于(inti=0;i我使用了ridsatrio的答案和一个更老的问题

我使用以下类从下面的字符串数组中获取国家代码:

import android.widget.TextView;

/**
 * Created by Friso on 14/11/21.
 */
public final class PhoneFunctions {
    private static PhoneFunctions instance;

    private PhoneFunctions(){}

    public static PhoneFunctions getInstance() {
        if (instance == null) {
            instance = new PhoneFunctions();
        }

        return instance;
    }

    public String getCountry(String[] argStringArray, TextView argText){
        String country="";

        if (argText.getText().toString().length() >= 4){
            for(int i=0;i<argStringArray.length;i++){
                String[] g=argStringArray[i].split(",");
                if(g[0].equals(getFirstFourChar(argText))){
                    country=g[1];
                    break;
                }
                if (g[0].equals(getFirstThreeChar(argText))){
                    country=g[1];
                    break;
                }
                if (g[0].equals(getFirstTwoChar(argText))){
                    country=g[1];
                    break;
                }
            }
        }

        return country;
    }

    public String getFirstFourChar(TextView argText){
        String threeChar;
        String text = argText.getText().toString();
        threeChar = text.substring(0,4);

        return threeChar;
    }

    public String getFirstThreeChar(TextView argText){
        String twoChar;
        String text = argText.getText().toString();
        twoChar = text.substring(0,3);

        return twoChar;
    }

    public String getFirstTwoChar(TextView argText){
        String oneChar;
        String text = argText.getText().toString();
        oneChar = text.substring(0,2);

        return oneChar;
    }
}
导入android.widget.TextView;
/**
*弗里索于21年11月14日创建。
*/
公共最终类函数{
私有静态电话功能实例;
专用PhoneFunctions(){}
公共静态函数getInstance(){
if(实例==null){
实例=新的PhoneFunctions();
}
返回实例;
}
公共字符串getCountry(字符串[]argStringArray,TextView argText){
字符串country=“”;
如果(argText.getText().toString().length()>=4){

对于(inti=0;i我使用了ridsatrio的答案和一个更老的问题

我使用以下类从下面的字符串数组中获取国家代码:

import android.widget.TextView;

/**
 * Created by Friso on 14/11/21.
 */
public final class PhoneFunctions {
    private static PhoneFunctions instance;

    private PhoneFunctions(){}

    public static PhoneFunctions getInstance() {
        if (instance == null) {
            instance = new PhoneFunctions();
        }

        return instance;
    }

    public String getCountry(String[] argStringArray, TextView argText){
        String country="";

        if (argText.getText().toString().length() >= 4){
            for(int i=0;i<argStringArray.length;i++){
                String[] g=argStringArray[i].split(",");
                if(g[0].equals(getFirstFourChar(argText))){
                    country=g[1];
                    break;
                }
                if (g[0].equals(getFirstThreeChar(argText))){
                    country=g[1];
                    break;
                }
                if (g[0].equals(getFirstTwoChar(argText))){
                    country=g[1];
                    break;
                }
            }
        }

        return country;
    }

    public String getFirstFourChar(TextView argText){
        String threeChar;
        String text = argText.getText().toString();
        threeChar = text.substring(0,4);

        return threeChar;
    }

    public String getFirstThreeChar(TextView argText){
        String twoChar;
        String text = argText.getText().toString();
        twoChar = text.substring(0,3);

        return twoChar;
    }

    public String getFirstTwoChar(TextView argText){
        String oneChar;
        String text = argText.getText().toString();
        oneChar = text.substring(0,2);

        return oneChar;
    }
}
导入android.widget.TextView;
/**
*弗里索于21年11月14日创建。
*/
公共最终类函数{
私有静态电话功能实例;
专用PhoneFunctions(){}
公共静态函数getInstance(){
if(实例==null){
实例=新的PhoneFunctions();
}
返回实例;
}
公共字符串getCountry(字符串[]argStringArray,TextView argText){
字符串country=“”;
如果(argText.getText().toString().length()>=4){

对于(int i=0;i您可以通过检查以“+”符号开头的手机号码来拆分您的手机号码,并且手机号码的长度应为10

void splitMobilenumberMethod(){
    String phoneNumb = MOBILE_NUMBER_TO_SPLIT;
    String ext = "", phoneN = "";
    if (phoneNumb.startsWith("+") || phoneNumb.length() > 10) {
        ext=phoneNumb.substring(0, 3);
        phoneN=phoneNumb.substring(3);
     } else {
        ext = "";
        phoneN = phoneNumb;
     }
     showSelectedPhoneDialog(ext, phoneN);
}


void showSelectedPhoneDialog(String ext, String phone) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("Verify Phone Number");
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.HORIZONTAL);
    layout.setGravity(View.TEXT_ALIGNMENT_CENTER);
    final EditText extEdit = new EditText(context);
    final EditText phoneEdit = new EditText(context);
    extEdit.setHint("Country");
    phoneEdit.setHint("Mobile Number");
    layout.addView(extEdit);
    layout.addView(phoneEdit);
    extEdit.setText(ext);
    phoneEdit.setText(phone);
    alertDialog.setView(layout);

    alertDialog.setIcon(R.drawable.ic_message);
    alertDialog.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do if split is correct or after make it corrent manually by user
        }
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            dialog.cancel();
        }
    });

    alertDialog.show();
}

您可以通过检查以“+”号开头的手机号码和手机号码的长度10来拆分手机号码

void splitMobilenumberMethod(){
    String phoneNumb = MOBILE_NUMBER_TO_SPLIT;
    String ext = "", phoneN = "";
    if (phoneNumb.startsWith("+") || phoneNumb.length() > 10) {
        ext=phoneNumb.substring(0, 3);
        phoneN=phoneNumb.substring(3);
     } else {
        ext = "";
        phoneN = phoneNumb;
     }
     showSelectedPhoneDialog(ext, phoneN);
}


void showSelectedPhoneDialog(String ext, String phone) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("Verify Phone Number");
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.HORIZONTAL);
    layout.setGravity(View.TEXT_ALIGNMENT_CENTER);
    final EditText extEdit = new EditText(context);
    final EditText phoneEdit = new EditText(context);
    extEdit.setHint("Country");
    phoneEdit.setHint("Mobile Number");
    layout.addView(extEdit);
    layout.addView(phoneEdit);
    extEdit.setText(ext);
    phoneEdit.setText(phone);
    alertDialog.setView(layout);

    alertDialog.setIcon(R.drawable.ic_message);
    alertDialog.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do if split is correct or after make it corrent manually by user
        }
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            dialog.cancel();
        }
    });

    alertDialog.show();
}

您可以通过检查以“+”号开头的手机号码和手机号码的长度10来拆分手机号码

void splitMobilenumberMethod(){
    String phoneNumb = MOBILE_NUMBER_TO_SPLIT;
    String ext = "", phoneN = "";
    if (phoneNumb.startsWith("+") || phoneNumb.length() > 10) {
        ext=phoneNumb.substring(0, 3);
        phoneN=phoneNumb.substring(3);
     } else {
        ext = "";
        phoneN = phoneNumb;
     }
     showSelectedPhoneDialog(ext, phoneN);
}


void showSelectedPhoneDialog(String ext, String phone) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("Verify Phone Number");
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.HORIZONTAL);
    layout.setGravity(View.TEXT_ALIGNMENT_CENTER);
    final EditText extEdit = new EditText(context);
    final EditText phoneEdit = new EditText(context);
    extEdit.setHint("Country");
    phoneEdit.setHint("Mobile Number");
    layout.addView(extEdit);
    layout.addView(phoneEdit);
    extEdit.setText(ext);
    phoneEdit.setText(phone);
    alertDialog.setView(layout);

    alertDialog.setIcon(R.drawable.ic_message);
    alertDialog.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do if split is correct or after make it corrent manually by user
        }
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            dialog.cancel();
        }
    });

    alertDialog.show();
}

您可以通过检查以“+”号开头的手机号码和手机号码的长度10来拆分手机号码

void splitMobilenumberMethod(){
    String phoneNumb = MOBILE_NUMBER_TO_SPLIT;
    String ext = "", phoneN = "";
    if (phoneNumb.startsWith("+") || phoneNumb.length() > 10) {
        ext=phoneNumb.substring(0, 3);
        phoneN=phoneNumb.substring(3);
     } else {
        ext = "";
        phoneN = phoneNumb;
     }
     showSelectedPhoneDialog(ext, phoneN);
}


void showSelectedPhoneDialog(String ext, String phone) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("Verify Phone Number");
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.HORIZONTAL);
    layout.setGravity(View.TEXT_ALIGNMENT_CENTER);
    final EditText extEdit = new EditText(context);
    final EditText phoneEdit = new EditText(context);
    extEdit.setHint("Country");
    phoneEdit.setHint("Mobile Number");
    layout.addView(extEdit);
    layout.addView(phoneEdit);
    extEdit.setText(ext);
    phoneEdit.setText(phone);
    alertDialog.setView(layout);

    alertDialog.setIcon(R.drawable.ic_message);
    alertDialog.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do if split is correct or after make it corrent manually by user
        }
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            dialog.cancel();
        }
    });

    alertDialog.show();
}

举例说明您使用的数据格式国家代码的长度或空格数不一致。您必须根据具体情况提出一种方法(例如,US仅为“1”,而DR为“1809”),因此您不能仅取第一组数字u