- android EditText ,keyboard textWatcher problem
- public class AppHome extends AppBaseActivity {
- private EditText ed = null;
- private NumberFormat amountFormatter = null;
- private boolean isUserInput = true;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.app_home_screen);
- ed = (EditText)findViewById(R.id.main_amount_textfield);
- amountFormatter = new DecimalFormat("##,##,###");
- ed.setOnKeyListener(new View.OnKeyListener() {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- if(event.getAction() == KeyEvent.ACTION_DOWN)
- return true;
- String strippedAmount = ed.getText().toString().replace(",", "");
- if(keyCode == KeyEvent.KEYCODE_DEL){
- //delete pressed, strip number of comas and then delete least significant digit.
- strippedAmount = strippedAmount.substring(0, strippedAmount.length() - 1);
- int amountNumeral = 0;
- try{
- amountNumeral = Integer.parseInt(strippedAmount);
- } catch(NumberFormatException e){
- }
- myObject.amount = amountNumeral;
- isUserInput = false;
- setFormattedAmount(amountNumeral,ed.getId());
- }else if(keyCode == KeyEvent.KEYCODE_ENTER){
- //enter pressed, save edits and resign keyboard
- int amountNumeral = 0;
- try{
- amountNumeral = Integer.parseInt(strippedAmount);
- } catch(NumberFormatException e){
- }
- myObject.amount = amountNumeral;
- isUserInput = false;
- setFormattedAmount(myObject.amount,ed.getId());
- //save edits
- save();
- //resign keyboard..
- InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
- in.hideSoftInputFromWindow(AppHome.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
- }
- return true;
- }
- });
- TextWatcher inputTextWatcher = new TextWatcher() {
- public void afterTextChanged(Editable s) {
- if(isUserInput == false){
- //textWatcher is recursive. When editText value is changed from code textWatcher callback gets called. So this variable acts as a flag which tells whether change is user generated or not..Possibly buggy code..:(
- isUserInput = true;
- return;
- }
- String strippedAmount = ed.getText().toString().replace(",", "");
- int amountNumeral = 0;
- try{
- amountNumeral = Integer.parseInt(strippedAmount);
- } catch(NumberFormatException e){
- }
- isUserInput = false;
- setFormattedAmount(amountNumeral,ed.getId());
- }
- public void beforeTextChanged(CharSequence s, int start, int count, int after){
- }
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- }
- };
- ed.addTextChangedListener(inputTextWatcher);
- }//end of onCreate...
- public void setFormattedAmount(Integer amount, Integer inputBoxId){
- double amountValue = 0;
- String textString =null;
- TextView amountInputBox = (TextView) findViewById(inputBoxId);
- amountValue = Double.parseDouble(Integer.toString(amount));
- textString = amountFormatter.format(amountValue).toString();
- amountInputBox.setText(textString);
- }
- }
- InputFilter filter = new InputFilter() {
- public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
- String strippedAmount = dest.toString() + source;
- strippedAmount = strippedAmount.replace(",", "");
- int amountNumeral = 0;
- try{
- amountNumeral = Integer.parseInt(strippedAmount);
- } catch(NumberFormatException e){
- }
- return amountFormatter.format(amountNumeral).toString();
- }
- };
- ed.setFilters(new InputFilter[]{filter});
- myObject.amount = 1234;
- ed.setText(amountFormatter.format(myObject.amount).toString());
- TextWatcher inputTextWatcher = new TextWatcher() {
- public void afterTextChanged(Editable s) {
- if(isUserInput == false){
- //textWatcher is recursive. When editText value is changed from code textWatcher callback gets called. So this variable acts as a flag which tells whether change is user generated or not..Possibly buggy code..:(
- isUserInput = true;
- ed.setSelection(ed.getText().length());
- return;
- }
- String strippedAmount = ed.getText().toString().replace(",", "");
- int amountNumeral = 0;
- try{
- amountNumeral = Integer.parseInt(strippedAmount);
- } catch(NumberFormatException e){
- }
- isUserInput = false;
- setFormattedAmount(amountNumeral,ed.getId());
- }
- public void beforeTextChanged(CharSequence s, int start, int count, int after){
- }
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- }
- };
- ed.addTextChangedListener(inputTextWatcher);
- ed.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- int length = ed.getText().length();
- ed.setCursorVisible(true);
- ed.setSelection(length);
- }
- });
- ed.setOnKeyListener(new View.OnKeyListener() {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- if(event.getAction() == KeyEvent.ACTION_UP)
- return true;
- String strippedAmount = ed.getText().toString().replace(",", "");
- if(keyCode == KeyEvent.KEYCODE_DEL){
- //delete pressed, strip number of comas and then delete least significant digit.
- strippedAmount = strippedAmount.substring(0, strippedAmount.length() - 1);
- int amountNumeral = 0;
- try{
- amountNumeral = Integer.parseInt(strippedAmount);
- } catch(NumberFormatException e){
- }
- isUserInput = false;
- setFormattedAmount(amountNumeral,ed.getId());
- return true;
- }else if(keyCode == KeyEvent.KEYCODE_ENTER){
- //enter pressed, save edits and resign keyboard
- int amountNumeral = 0;
- try{
- amountNumeral = Integer.parseInt(strippedAmount);
- } catch(NumberFormatException e){
- }
- isUserInput = false;
- setFormattedAmount(amountNumeral,ed.getId());
- //save edits
- //resign keyboard..
- InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
- in.hideSoftInputFromWindow(AppHome.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
- return true;
- }
- return false;
- }
- });
- /**
- * This filter will capitalize all the lower case letters that are added
- * through edits.
- */
- public static class AllCaps implements InputFilter {
- public CharSequence filter(CharSequence source, int start, int end,
- Spanned dest, int dstart, int dend) {
- for (int i = start; i < end; i++) {
- if (Character.isLowerCase(source.charAt(i))) {
- char[] v = new char[end - start];
- TextUtils.getChars(source, start, end, v, 0);
- String s = new String(v).toUpperCase();
- if (source instanceof Spanned) {
- SpannableString sp = new SpannableString(s);
- TextUtils.copySpansFrom((Spanned) source,
- start, end, null, sp, 0);
- return sp;
- } else {
- return s;
- }
- }
- }
- return null; // keep original
- }
- }