Ti posto il codice di prova:
package com.example.prova.text;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity extends Activity {
public EditText number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextWatcher watcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// 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 afterTextChanged(Editable s) {
if (s.toString().trim().length() == 0){
return;
}
number.removeTextChangedListener(this);
DecimalFormat format = new DecimalFormat();
format.setGroupingUsed(true);
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator('.');
format.setDecimalFormatSymbols(symbols);
String sn = s.toString().replace(",","");
double n = Double.parseDouble(sn);
Log.d("TAG", "Numero = "+n);
number.setText(format.format(n));
number.setSelection(number.getText().length());
number.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}