8

Tôi đã là edittext, nó chỉ có số không có số thập phân.Cách định dạng đầu vào của EditText khi nhập với hàng nghìn dấu tách (,) trong Android?

android:inputType="number" 

Tôi muốn tách biệt hàng nghìn khi đang nhập. Ví dụ 25.000.

Tôi biết tôi nên sử dụng TextWatcher và tôi đã sử dụng mã này, nhưng tôi không thể làm cho nó hoạt:

@Override 
     public void afterTextChanged(Editable viewss) { 
      String s = null; 
      try { 
       // The comma in the format specifier does the trick 
       s = String.format("%,d", Long.parseLong(viewss.toString())); 
      } catch (NumberFormatException e) { 
      } 

     } 

Ông có thể giúp tôi làm như vậy?

Trả lời

11

thử ví dụ này:

import java.text.DecimalFormat; 
import java.text.ParseException; 

import android.text.Editable; 
import android.text.TextWatcher; 
import android.widget.EditText; 

public class NumberTextWatcher implements TextWatcher { 

    private DecimalFormat df; 
    private DecimalFormat dfnd; 
    private boolean hasFractionalPart; 

    private EditText et; 

    public NumberTextWatcher(EditText et) 
    { 
     df = new DecimalFormat("#,###.##"); 
     df.setDecimalSeparatorAlwaysShown(true); 
     dfnd = new DecimalFormat("#,###"); 
     this.et = et; 
     hasFractionalPart = false; 
    } 

    @SuppressWarnings("unused") 
    private static final String TAG = "NumberTextWatcher"; 

    @Override 
    public void afterTextChanged(Editable s) 
    { 
     et.removeTextChangedListener(this); 

     try { 
      int inilen, endlen; 
      inilen = et.getText().length(); 

      String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), ""); 
      Number n = df.parse(v); 
      int cp = et.getSelectionStart(); 
      if (hasFractionalPart) { 
       et.setText(df.format(n)); 
      } else { 
       et.setText(dfnd.format(n)); 
      } 
      endlen = et.getText().length(); 
      int sel = (cp + (endlen - inilen)); 
      if (sel > 0 && sel <= et.getText().length()) { 
       et.setSelection(sel); 
      } else { 
       // place cursor at the end? 
       et.setSelection(et.getText().length() - 1); 
      } 
     } catch (NumberFormatException nfe) { 
      // do nothing? 
     } catch (ParseException e) { 
      // do nothing? 
     } 

     et.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) 
    { 
     if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()))) 
     { 
      hasFractionalPart = true; 
     } else { 
      hasFractionalPart = false; 
     } 
    } 

} 

Để sử dụng nó, thêm một TextChangedListener với thành phần EditText.

editText.addTextChangedListener(new NumberTextWatcher(editText)); 
+0

Cách thêm số 0 vào văn bản? Tôi muốn hiển thị một văn bản như 0939 – FarshidABZ

+0

@FarshidABZ Tôi không biết tôi không làm việc với Android tại thời điểm này. –

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