ユーザーが小数を含む数値を入力する必要があるEditTextがあり、入力数値に3桁の区切り記号を自動的に追加したい他のいくつかの方法を試しましたが、浮動小数点数を許可しないものもあるので、動作するこのコードを思いつきました文字列入力がリアルタイムで編集されていないため、1000 単位の区切り記号が使用されている可能性があり、エラーは s.replace(); に起因するようです。
am2 = new TextWatcher(){
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
public void afterTextChanged(Editable s) {
if (s.toString().equals("")) {
amount.setText("");
value = 0;
}else{
StringBuffer strBuff = new StringBuffer();
char c;
for (int i = 0; i < amount2.getText().toString().length() ; i++) {
c = amount2.getText().toString().charAt(i);
if (Character.isDigit(c)) {
strBuff.append(c);
}
}
value = Double.parseDouble(strBuff.toString());
reverse();
NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
((DecimalFormat)nf2).applyPattern("###,###.#######");
s.replace(0, s.length(), nf2.format(value));
}
}
};