[안드로이드,Android] 3자리수(세자리마다) , 컴마 찍기 천단위 금액 , 컴마찍기

2013. 1. 4. 15:45Mobile/Android



반응형

[안드로이드,Android] 3자리수(세자리마다) , 컴마 찍기 천단위 금액  , 컴마찍기

 

 

 

package com.example.teste22;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 *
 * @author KYJ 3자리수 컴마 100단위 컴마찍기
 */
public class MainActivity extends Activity {

 EditText et01;
 String strAmount = ""; // 임시저장값 (콤마)

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  et01 = (EditText) findViewById(R.id.et01);

  et01.addTextChangedListener(new TextWatcher() {
   @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (!s.toString().equals(strAmount)) { // StackOverflow를 막기위해,
     strAmount = makeStringComma(s.toString().replace(",", ""));
     et01.setText(strAmount);
     Editable e = et01.getText();
     Selection.setSelection(e, strAmount.length());
    }
   }

   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,int after) { }

   @Override
   public void afterTextChanged(Editable s) { }});

 }

 protected String makeStringComma(String str) {
  if (str.length() == 0)
   return "";
  long value = Long.parseLong(str);
  DecimalFormat format = new DecimalFormat("###,###");
  return format.format(value);
 }
}

반응형