Phone number formatting an EditText in Android

前端 未结 14 962
情深已故
情深已故 2020-12-02 12:03

I am making a simple Address Book app (targeting 4.2) that takes name, address, city, state, zip and phone.

I want to format the phone number input as a phone number

14条回答
  •  醉酒成梦
    2020-12-02 12:21

    Don't worry. I have make a most of better solution for you. You can see this simple app link below.

    private EditText mPasswordField;
    public int textLength = 0;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mPasswordField = (EditText) findViewById(R.id.password_field);
        mPasswordField.addTextChangedListener(this);
    }
    
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    
    
        String text = mPasswordField.getText().toString();
        textLength = mPasswordField.getText().length();
    
        if (text.endsWith("-") || text.endsWith(" ") || text.endsWith(" "))
            return;
    
        if (textLength == 1) {
            if (!text.contains("(")) {
                mPasswordField.setText(new StringBuilder(text).insert(text.length() - 1, "(").toString());
                mPasswordField.setSelection(mPasswordField.getText().length());
            }
    
        } else if (textLength == 5) {
    
            if (!text.contains(")")) {
                mPasswordField.setText(new StringBuilder(text).insert(text.length() - 1, ")").toString());
                mPasswordField.setSelection(mPasswordField.getText().length());
            }
    
        } else if (textLength == 6) {
                mPasswordField.setText(new StringBuilder(text).insert(text.length() - 1, " ").toString());
                mPasswordField.setSelection(mPasswordField.getText().length());
    
        } else if (textLength == 10) {
            if (!text.contains("-")) {
                mPasswordField.setText(new StringBuilder(text).insert(text.length() - 1, "-").toString());
                mPasswordField.setSelection(mPasswordField.getText().length());
            }
        } else if (textLength == 15) {
            if (text.contains("-")) {
                mPasswordField.setText(new StringBuilder(text).insert(text.length() - 1, "-").toString());
                mPasswordField.setSelection(mPasswordField.getText().length());
            }
        }else if (textLength == 18) {
            if (text.contains("-")) {
                mPasswordField.setText(new StringBuilder(text).insert(text.length() - 1, "-").toString());
                mPasswordField.setSelection(mPasswordField.getText().length());
            }
        } else if (textLength == 20) {
            Intent i = new Intent(MainActivity.this, Activity2.class);
            startActivity(i);
    
        }
    
    
    
    }
    
    @Override
    public void afterTextChanged(Editable s) {
    
    }
    

    Not: Don't forget "implement TextWatcher" with your activity class.

    Link :https://drive.google.com/open?id=0B-yo9VvU7jyBMjJpT29xc2k5bnc

    Hope you are feeling cool for this solution.

提交回复
热议问题