2010-11-11 35 views
17

Tôi muốn làm cho số điện thoại được nhập của người dùng trong editText thay đổi động mỗi khi người dùng nhập số. Tức là, khi người dùng nhập vào tối đa 4 chữ số, như 7144, editText hiển thị "714-4". Tôi muốn editText được cập nhật động thành định dạng ### - ### - #### bất cứ khi nào người dùng nhập một chữ số. Điều này có thể giải quyết như thế nào? ngoài ra, tôi đang xử lý nhiều editTexts.Mặt nạ EditText có Định dạng Số Điện thoại NaN như trong PhoneNumberUtils

Trả lời

59

Cách dễ nhất để thực hiện việc này là sử dụng ứng dụng được tích hợp sẵn trong Android PhoneNumberFormattingTextWatcher.

Vì vậy, về cơ bản bạn nhận được EditText của bạn trong mã và thiết lập quan sát văn bản của bạn như thế này ...

EditText inputField = (EditText) findViewById(R.id.inputfield); 
inputField.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); 

đẹp điều về việc sử dụng PhoneNumberFormattingTextWatcher là nó sẽ định dạng nhập số của bạn một cách chính xác dựa trên miền địa phương của bạn.

+1

Nếu bạn mong muốn một mặt nạ tùy chỉnh, bạn có thể tìm thấy câu trả lời này hữu ích: http://stackoverflow.com/a/34907607/1013929 –

6

Câu trả lời trên là đúng nhưng hoạt động với quốc gia cụ thể. nếu có ai muốn số điện thoại được định dạng như vậy (### - ### - ####). Sau đó, sử dụng điều này:

etPhoneNumber.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
        int digits = etPhoneNumber.getText().toString().length(); 
        if (digits > 1) 
         lastChar = etPhoneNumber.getText().toString().substring(digits-1); 
       } 

       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
        int digits = etPhoneNumber.getText().toString().length(); 
        Log.d("LENGTH",""+digits); 
        if (!lastChar.equals("-")) { 
         if (digits == 3 || digits == 7) { 
          etPhoneNumber.append("-"); 
         } 
        } 
       } 

       @Override 
       public void afterTextChanged(Editable s) { 

       } 
      }); 

Tuyên bố String lastChar = " " trong hoạt động của bạn.

Bây giờ thêm dòng này trong xml của EditText bạn

android:inputType="phone" 

Đó là tất cả.

được sửa đổi: Nếu bạn muốn chiều dài EditText của bạn để hạn chế 10 chữ số thêm dòng dưới đây cũng:

android:maxLength="12" 

(Đó là 12 vì "-" sẽ mất không gian hai lần)

+0

này làm việc cho XXX-XXX -XXXX định dạng số điện thoại. Chỉ cần sao chép dán. –

1

Chỉ cần thêm sau đây để EditText cho số điện thoại để có được một số điện thoại được định dạng (### - ### - ####)

Phone.addTextChangedListener(new TextWatcher() { 

     int length_before = 0; 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      length_before = s.length(); 
     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      if (length_before < s.length()) { 
       if (s.length() == 3 || s.length() == 7) 
        s.append("-"); 
       if (s.length() > 3) { 
        if (Character.isDigit(s.charAt(3))) 
         s.insert(3, "-"); 
       } 
       if (s.length() > 7) { 
        if (Character.isDigit(s.charAt(7))) 
         s.insert(7, "-"); 
       } 
      } 
     } 
    }); 
1

kịch bản của tôi, ví dụ lấy từ đây description here


<android.support.design.widget.TextInputLayout 
    android:id="@+id/numphone_layout" 
    app:hintTextAppearance="@style/MyHintText" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 

    android:layout_marginTop="8dp"> 

    <android.support.design.widget.TextInputEditText 
     android:id="@+id/edit_text_numphone" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/MyEditText" 
     android:digits="+() 1234567890-" 
     android:hint="@string/hint_numphone" 
     android:inputType="phone" 
     android:maxLength="17" 
     android:textSize="14sp" /> 
</android.support.design.widget.TextInputLayout> 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
TextInputEditText phone = (TextInputEditText) findViewById(R.id.edit_text_numphone); 
//Add to mask 
    phone.addTextChangedListener(textWatcher); 
} 


    TextWatcher textWatcher = new TextWatcher() { 
    private boolean mFormatting; // this is a flag which prevents the stack overflow. 
    private int mAfter; 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     // nothing to do here.. 
    } 

    //called before the text is changed... 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     //nothing to do here... 
     mAfter = after; // flag to detect backspace.. 

    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     // Make sure to ignore calls to afterTextChanged caused by the work done below 
     if (!mFormatting) { 
      mFormatting = true; 
      // using US or RU formatting... 
      if(mAfter!=0) // in case back space ain't clicked... 
      { 
       String num =s.toString(); 
       String data = PhoneNumberUtils.formatNumber(num, "RU"); 
       if(data!=null) 
       { 
        s.clear(); 
        s.append(data); 
        Log.i("Number", data);//8 (999) 123-45-67 or +7 999 123-45-67 
       } 

      } 
      mFormatting = false; 
     } 
    } 
}; 
+0

Đây là giải pháp tốt nhất duy nhất hoạt động như sự quyến rũ – Chandru

Các vấn đề liên quan