2017-01-24 57 views
0

我想單獨編輯像這樣的文本(9,99,999)。我在網上搜索了這個,但我還沒有找到適當的解決方案。如何在Android的編輯文本中使用千行分隔符........?

MainActivity.java

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     EditText editText = (EditText) findViewById(R.id.edittext); 
     editText.addTextChangedListener(new NumberTextWatcherForThousand(editText)); NumberTextWatcherForThousand.trimCommaOfString(editText.getText().toString()); 
} 
} 

NumberTextWatcherForThousand

public class NumberTextWatcherForThousand implements TextWatcher { 
    EditText editText; 
    public NumberTextWatcherForThousand(EditText editText) { 
     this.editText = editText; 
    } 

    @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) { 
     try 
     { 
      editText.removeTextChangedListener(this); 
      String value = editText.getText().toString(); 


      if (value != null && !value.equals("")) 
      { 

       if(value.startsWith(".")){ 
        editText.setText("0."); 
       } 
       if(value.startsWith("0") && !value.startsWith("0.")){ 
        editText.setText(""); 

       } 


       String str = editText.getText().toString().replaceAll(",", ""); 
       if (!value.equals("")) 
        editText.setText(getDecimalFormattedString(str)); 
       editText.setSelection(editText.getText().toString().length()); 
      } 
      editText.addTextChangedListener(this); 
      return; 
     } 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
      editText.addTextChangedListener(this); 
     } 

    } 

    public static String getDecimalFormattedString(String value) 
    { 
     StringTokenizer lst = new StringTokenizer(value, "."); 
     String str1 = value; 
     String str2 = ""; 
     if (lst.countTokens() > 1) 
     { 
      str1 = lst.nextToken(); 
      str2 = lst.nextToken(); 
     } 
     String str3 = ""; 
     int i = 0; 
     int j = -1 + str1.length(); 
     if (str1.charAt(-1 + str1.length()) == '.') 
     { 
      j--; 
      str3 = "."; 
     } 
     for (int k = j;; k--) 
     { 
      if (k < 0) 
      { 
       if (str2.length() > 0) 
        str3 = str3 + "." + str2; 
       return str3; 
      } 
      if (i == 3) 
      { 
       str3 = "," + str3; 
       i = 0; 
      } 
      str3 = str1.charAt(k) + str3; 
      i++; 
     } 

    } 

    public static String trimCommaOfString(String string) { 
     if(string.contains(",")){ 
      return string.replace(",","");} 
     else { 
      return string; 
     } 

    } 
} 
+0

Oracle提供的數字格式的一些選項。你可能想檢查一些類似的機器人。我發現http://docs.oracle.com/javase/tutorial/java/data/numberformat.html的鏈接之一 – denis

回答

0

試試這個下面的代碼: -

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; 
     } 
    } 

} 

,並在您的EditText

editText.addTextChangedListener(new NumberTextWatcher(editText)); 
1

這將設置文本的格式並在編輯文本中的千位添加逗號。

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

在附加的build.gradle以下行

repositories{ 
maven { url "https://jitpack.io" } 

}

dependencies { 
compile 'com.github.BlacKCaT27:CurrencyEditText:v1.4.4' 

}

相反的EditText使用下面的代碼

<com.blackcat.currencyedittext.CurrencyEditText 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
/> 

檢查此鏈接https://github.com/BlacKCaT27/CurrencyEditText