edt1 = (EditText) findViewById(R.id.editText); edt1.addTextChangedListener(new CustomTextWatcher(edt1));
클래스 생성
=======================================================
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.widget.EditText;
import java.text.DecimalFormat;
/**
* Created by buster on 17. 10. 13.
* 천단위 컴마 붙이기
*
*/
public class CustomTextWatcher implements TextWatcher {
@SuppressWarnings("unused")
private EditText mEditText;
String strAmount = ""; // 임시 저장값 (콤마)
public CustomTextWatcher(EditText e) {
mEditText = e;
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { //텍스트가 변경될때마다 실행
if (!s.toString().equals(strAmount)) { // StackOverflow 방지
strAmount = makeStringComma(s.toString().replace(",", ""));
mEditText.setText(strAmount);
Editable e = mEditText.getText();
Selection.setSelection(e, strAmount.length());
}
}
protected String makeStringComma(String str) { // 천단위 콤마 처리
if (str.length() == 0)
return "";
long value = Long.parseLong(str);
DecimalFormat format = new DecimalFormat("###,###");
return format.format(value);
}
}
======================================================
계산식에 , 마 제거 해야 정상적인 계산이 이루어 진다.
double input1 = Double.valueOf(edt1.getText().toString().replace(",",""));
댓글 없음:
댓글 쓰기