android edittext textwatcher format phone number like xxx-xxx-xx-xx

前端 未结 2 1842
孤独总比滥情好
孤独总比滥情好 2020-12-22 07:31

how to format phone number like xxx-xxx-xx-xx using textwacher tried following code,bt its not working while i delete elements



        
相关标签:
2条回答
  • 2020-12-22 07:51

    Use Masked-EditText library.

    Just pass your mask like below.

    <com.github.pinball83.maskededittext.MaskedEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        app:mask="xxx-xxx-xx-xx"
        app:notMaskedSymbol="x"
        app:maskIconColor="@color/colorPrimary" />
    

    Note : If you want to remove incorrect number than place your cursor before that digit and enter your right digit this Masked-EditText library will change it.

    0 讨论(0)
  • 2020-12-22 07:52

    finally i did it like this :

    public class PhoneNumberTextWatcher implements TextWatcher {
    
    private static final String TAG = PhoneNumberTextWatcher.class
            .getSimpleName();
    private EditText edTxt;
    private boolean isDelete;
    
    public PhoneNumberTextWatcher(EditText edTxtPhone) {
        this.edTxt = edTxtPhone;
        edTxt.setOnKeyListener(new OnKeyListener() {
    
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_DEL) {
                    isDelete = true;
                }
                return false;
            }
        });
    }
    
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
    
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }
    
    public void afterTextChanged(Editable s) {
    
        if (isDelete) {
            isDelete = false;
            return;
        }
        String val = s.toString();
        String a = "";
        String b = "";
        String c = "";
        if (val != null && val.length() > 0) {
            val = val.replace("-", "");
            if (val.length() >= 3) {
                a = val.substring(0, 3);
            } else if (val.length() < 3) {
                a = val.substring(0, val.length());
            }
            if (val.length() >= 6) {
                b = val.substring(3, 6);
                c = val.substring(6, val.length());
            } else if (val.length() > 3 && val.length() < 6) {
                b = val.substring(3, val.length());
            }
            StringBuffer stringBuffer = new StringBuffer();
            if (a != null && a.length() > 0) {
                stringBuffer.append(a);
                if (a.length() == 3) {
                    stringBuffer.append("-");
                }
            }
            if (b != null && b.length() > 0) {
                stringBuffer.append(b);
                if (b.length() == 3) {
                    stringBuffer.append("-");
                }
            }
            if (c != null && c.length() > 0) {
                stringBuffer.append(c);
            }
            edTxt.removeTextChangedListener(this);
            edTxt.setText(stringBuffer.toString());
            edTxt.setSelection(edTxt.getText().toString().length());
            edTxt.addTextChangedListener(this);
        } else {
            edTxt.removeTextChangedListener(this);
            edTxt.setText("");
            edTxt.addTextChangedListener(this);
        }
    
    }
     }
    

    MainActivity.class

    et_phone_num = (EditText) findViewById(R.id.et_phone_num);
     et_phone_num.addTextChangedListener(new PhoneNumberTextWatcher(et_phone_num));
    

    activity_main.xml

     <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:visibility="visible"
            android:id="@+id/et_phone_num"
            android:inputType="phone"
            android:maxLength="12"
            android:digits="0123456789"
            android:background="@drawable/phone_edittext_drawable"
            android:gravity="center"
            android:hint="5XX-XXX-XXXX"
            android:imeOptions="actionDone"
            android:textColor="@color/cc" />
    
    0 讨论(0)
提交回复
热议问题