android中的edittext删除了手机格式

android中的edittext删除了手机格式,android,Android,伙计们,我有一个关于edittext的问题。我在edittext上创建了一个电话格式 当我运行应用程序时,它正在工作,但当我删除edittext上的数字时,格式被删除。你知道我能做什么吗 我的形象: 我的代码是: public class MainActivity extends AppCompatActivity implements KeyEvent.Callback { EditText edittext1; String befor="0 _ _ _ - _ _ _

伙计们,我有一个关于edittext的问题。我在edittext上创建了一个电话格式

当我运行应用程序时,它正在工作,但当我删除edittext上的数字时,格式被删除。你知道我能做什么吗

我的形象:

我的代码是:

    public class MainActivity extends AppCompatActivity implements KeyEvent.Callback {

   EditText edittext1;
   String befor="0 _ _ _ - _ _ _ _ _ _ _";
   TextView textview;
   int u,u1,u2,u3,u4,u5,a,u6,u7 ;
   String store;


@Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    edittext1=(EditText)findViewById(R.id.editText1);
    textview=(TextView)findViewById(R.id.textView);
    edittext1.setText(befor);
    edittext1.setSelection(1);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    //System.out.println(edittext1.length());


    edittext1.addTextChangedListener(textWatcher);



}

private final  TextWatcher textWatcher=new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {


    }

    @Override
    public void afterTextChanged(Editable s) {


        if (edittext1.length() == 24) {

            String ilk=s.toString();
            edittext1.setText(ilk.substring(0, 2) + "_ _ - _ _ _ _ _ _ _");
            edittext1.setSelection(2);
             a=1;
        } else if (edittext1.length() == 22 && a==1) {

             store = s.toString();
             edittext1.setText(store.substring(0, 4) + " - _ _ _ _ _ _ _");
             edittext1.setSelection(3);
             u=4;



        } else if (edittext1.length() == 21 && u ==4 ) {

            store = s.toString();
            u=5;
            edittext1.setText(store.substring(0,4) + " - _ _ _ _ _ _ _");
            edittext1.setSelection(6);
            u1=3;


        }
        else if (edittext1.length()==21 && u1==3) {
             store = s.toString();
            edittext1.setText(store.substring(0, 6)+" " + store.substring(6,7) + "_ _ _ _ _ _");
            edittext1.setSelection(8);

            u2=1;
        }
        else if (edittext1.length()==20 && u2==1) {
             store = s.toString();
            edittext1.setText(store.substring(0, 9) + "_ _ _ _ _");
            edittext1.setSelection(9);

            u3=1;
        }
        else if (edittext1.length()==19 && u3==1)
        {
            String store = s.toString();
            edittext1.setText(store.substring(0, 10) + "_ _ _ _");
            edittext1.setSelection(10);
            u4=1;
        }
        else if (edittext1.length()==18 && u4==1)
        {
             store = s.toString();
            u4=0;
            edittext1.setText(store.substring(0, 10)+" " + store.substring(10,11) + "_ _ _");
            edittext1.setSelection(12);
            u5=1;


        }


        else if (edittext1.length()==18 && u5==1)
        {
             store = s.toString();
            edittext1.setText(store.substring(0, 13) + "_ _");
            edittext1.setSelection(13);
            u6=1;

        }


        else if (edittext1.length()==17 && u6==1)
        {
            String store = s.toString();
            u6=2;
            edittext1.setText(store.substring(0, 13)+ " " + store.substring(13,14) + "_");
            edittext1.setSelection(15);
            u7=1;

        }

        else if (edittext1.length() == 17 && u7 == 1) {
            String store = s.toString();
            edittext1.setText(store.substring(0, 16) + "");
            edittext1.setSelection(16);
            }

        }
     };


    }

我建议您使用Android自己的
PhoneNumberFormattingTextWatcher
。当用户在您的
EditText
中键入号码时,它会自动将其格式化为您要查找的电话号码样式

edittext1.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
PhoneNumberFormattingTextWatcher
在后台使用此方法:

PhoneNumberUtils.formatNumber(Editable text, int formattingType);
在构造
PhoneNumberFormattingTextWatcher
时,会自动从用户的手机接收格式化类型,这意味着电话号码格式化将始终正确到用户所在的国家/地区。您可以从Google源代码中看到:

public PhoneNumberFormattingTextWatcher() {
    this(Locale.getDefault().getCountry());
}
如果要为所有用户指定特定格式,则只需使用第二个构造函数:

public PhoneNumberFormattingTextWatcher(String countryCode) {
    if (countryCode == null) throw new IllegalArgumentException();
    mFormatter = PhoneNumberUtil.getInstance().getAsYouTypeFormatter(countryCode);
}
edittext1.addTextChangedListener(new PhoneNumberFormattingTextWatcher(COUNTRY_CODE));
这将使用您指定的国家/地区代码,因此在添加
TextChangedListener
时,您将在构造函数中指定:

public PhoneNumberFormattingTextWatcher(String countryCode) {
    if (countryCode == null) throw new IllegalArgumentException();
    mFormatter = PhoneNumberUtil.getInstance().getAsYouTypeFormatter(countryCode);
}
edittext1.addTextChangedListener(new PhoneNumberFormattingTextWatcher(COUNTRY_CODE));
但是
PhoneNumberFormattingTextWatcher
也有一些细节,可以在下面的列表中看到:

int   FORMAT_JAPAN     Japanese formatting
int   FORMAT_NANP      NANP formatting
int   FORMAT_UNKNOWN   The current locale is unknown, look for a country code or don't format

您应该尝试PhoneNumberUtils类及其格式方法。我是否可以更改PhoneNumberFormattingTextWatcer使用的格式。我已进行了更多研究,并确认它将根据用户当前所在国家/地区进行更新。我再次回答了我的问题。“凯末尔,如果我的回答帮助你解决了问题,请考虑接受。谢谢